/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_merge.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
import contextlib
17
18
import os
18
 
from StringIO import StringIO
19
19
 
20
 
from bzrlib import (
 
20
from .. import (
 
21
    branch as _mod_branch,
21
22
    conflicts,
22
23
    errors,
23
 
    knit,
24
24
    memorytree,
25
25
    merge as _mod_merge,
26
26
    option,
27
 
    progress,
 
27
    revision as _mod_revision,
28
28
    tests,
29
29
    transform,
 
30
    )
 
31
from ..bzr import (
 
32
    inventory,
 
33
    knit,
30
34
    versionedfile,
31
35
    )
32
 
from bzrlib.conflicts import ConflictList, TextConflict
33
 
from bzrlib.errors import UnrelatedBranches, NoCommits
34
 
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
35
 
from bzrlib.osutils import pathjoin, file_kind
36
 
from bzrlib.tests import (
 
36
from ..conflicts import ConflictList
 
37
from ..bzr.conflicts import TextConflict, MissingParent, UnversionedParent, DeletingParent, ContentsConflict
 
38
from ..errors import UnrelatedBranches, NoCommits
 
39
from ..merge import transform_tree, merge_inner, _PlanMerge
 
40
from ..osutils import basename, pathjoin, file_kind
 
41
from . import (
 
42
    features,
37
43
    TestCaseWithMemoryTransport,
38
44
    TestCaseWithTransport,
39
45
    test_merge_core,
40
46
    )
41
 
from bzrlib.workingtree import WorkingTree
 
47
from ..workingtree import WorkingTree
42
48
 
43
49
 
44
50
class TestMerge(TestCaseWithTransport):
79
85
        tip = wt1.commit('empty commit')
80
86
        wt2 = self.make_branch_and_tree('branch2')
81
87
        wt2.pull(wt1.branch)
82
 
        file('branch1/foo', 'wb').write('foo')
83
 
        file('branch1/bar', 'wb').write('bar')
 
88
        with open('branch1/foo', 'wb') as f:
 
89
            f.write(b'foo')
 
90
        with open('branch1/bar', 'wb') as f:
 
91
            f.write(b'bar')
84
92
        wt1.add('foo')
85
93
        wt1.add('bar')
86
94
        wt1.commit('add foobar')
87
 
        os.chdir('branch2')
88
 
        self.run_bzr('merge ../branch1/baz', retcode=3)
89
 
        self.run_bzr('merge ../branch1/foo')
90
 
        self.failUnlessExists('foo')
91
 
        self.failIfExists('bar')
92
 
        wt2 = WorkingTree.open('.') # opens branch2
 
95
        self.run_bzr('merge ../branch1/baz', retcode=3, working_dir='branch2')
 
96
        self.run_bzr('merge ../branch1/foo', working_dir='branch2')
 
97
        self.assertPathExists('branch2/foo')
 
98
        self.assertPathDoesNotExist('branch2/bar')
 
99
        wt2 = WorkingTree.open('branch2')
93
100
        self.assertEqual([tip], wt2.get_parent_ids())
94
101
 
95
102
    def test_pending_with_null(self):
100
107
        br1.fetch(wt2.branch)
101
108
        # merge all of branch 2 into branch 1 even though they
102
109
        # are not related.
103
 
        wt1.merge_from_branch(wt2.branch, wt2.last_revision(), 'null:')
 
110
        wt1.merge_from_branch(wt2.branch, wt2.last_revision(), b'null:')
104
111
        self.assertEqual([br1.last_revision(), wt2.branch.last_revision()],
105
 
            wt1.get_parent_ids())
 
112
                         wt1.get_parent_ids())
106
113
        return (wt1, wt2.branch)
107
114
 
108
115
    def test_two_roots(self):
118
125
        finally:
119
126
            wt1.unlock()
120
127
 
 
128
    def test_merge_into_null_tree(self):
 
129
        wt = self.make_branch_and_tree('tree')
 
130
        null_tree = wt.basis_tree()
 
131
        self.build_tree(['tree/file'])
 
132
        wt.add('file')
 
133
        wt.commit('tree with root')
 
134
        merger = _mod_merge.Merge3Merger(null_tree, null_tree, null_tree, wt,
 
135
                                         this_branch=wt.branch,
 
136
                                         do_merge=False)
 
137
        with merger.make_preview_transform() as tt:
 
138
            self.assertEqual([], tt.find_raw_conflicts())
 
139
            preview = tt.get_preview_tree()
 
140
            self.assertEqual(wt.path2id(''), preview.path2id(''))
 
141
 
 
142
    def test_merge_unrelated_retains_root(self):
 
143
        wt = self.make_branch_and_tree('tree')
 
144
        other_tree = self.make_branch_and_tree('other')
 
145
        self.addCleanup(other_tree.lock_read().unlock)
 
146
        merger = _mod_merge.Merge3Merger(wt, wt, wt.basis_tree(), other_tree,
 
147
                                         this_branch=wt.branch,
 
148
                                         do_merge=False)
 
149
        with wt.preview_transform() as merger.tt:
 
150
            merger._compute_transform()
 
151
            new_root_id = merger.tt.final_file_id(merger.tt.root)
 
152
            self.assertEqual(wt.path2id(''), new_root_id)
 
153
 
121
154
    def test_create_rename(self):
122
155
        """Rename an inventory entry while creating the file"""
123
 
        tree =self.make_branch_and_tree('.')
124
 
        file('name1', 'wb').write('Hello')
 
156
        tree = self.make_branch_and_tree('.')
 
157
        with open('name1', 'wb') as f:
 
158
            f.write(b'Hello')
125
159
        tree.add('name1')
126
160
        tree.commit(message="hello")
127
161
        tree.rename_one('name1', 'name2')
130
164
 
131
165
    def test_layered_rename(self):
132
166
        """Rename both child and parent at same time"""
133
 
        tree =self.make_branch_and_tree('.')
 
167
        tree = self.make_branch_and_tree('.')
134
168
        os.mkdir('dirname1')
135
169
        tree.add('dirname1')
136
170
        filename = pathjoin('dirname1', 'name1')
137
 
        file(filename, 'wb').write('Hello')
 
171
        with open(filename, 'wb') as f:
 
172
            f.write(b'Hello')
138
173
        tree.add(filename)
139
174
        tree.commit(message="hello")
140
175
        filename2 = pathjoin('dirname1', 'name2')
144
179
 
145
180
    def test_ignore_zero_merge_inner(self):
146
181
        # Test that merge_inner's ignore zero parameter is effective
147
 
        tree_a =self.make_branch_and_tree('a')
 
182
        tree_a = self.make_branch_and_tree('a')
148
183
        tree_a.commit(message="hello")
149
 
        dir_b = tree_a.bzrdir.sprout('b')
 
184
        dir_b = tree_a.controldir.sprout('b')
150
185
        tree_b = dir_b.open_workingtree()
151
186
        tree_b.lock_write()
152
187
        self.addCleanup(tree_b.unlock)
153
188
        tree_a.commit(message="hello again")
154
 
        log = StringIO()
155
189
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
156
190
                    this_tree=tree_b, ignore_zero=True)
157
 
        self.failUnless('All changes applied successfully.\n' not in
158
 
            self.get_log())
 
191
        self.assertTrue('All changes applied successfully.\n' not in
 
192
                        self.get_log())
159
193
        tree_b.revert()
160
194
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
161
195
                    this_tree=tree_b, ignore_zero=False)
162
 
        self.failUnless('All changes applied successfully.\n' in self.get_log())
 
196
        self.assertTrue(
 
197
            'All changes applied successfully.\n' in self.get_log())
163
198
 
164
199
    def test_merge_inner_conflicts(self):
165
200
        tree_a = self.make_branch_and_tree('a')
166
 
        tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
 
201
        tree_a.set_conflicts([TextConflict('patha')])
167
202
        merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
168
203
        self.assertEqual(1, len(tree_a.conflicts()))
169
204
 
170
205
    def test_rmdir_conflict(self):
171
206
        tree_a = self.make_branch_and_tree('a')
172
207
        self.build_tree(['a/b/'])
173
 
        tree_a.add('b', 'b-id')
 
208
        tree_a.add('b', b'b-id')
174
209
        tree_a.commit('added b')
175
210
        # basis_tree() is only guaranteed to be valid as long as it is actually
176
211
        # the basis tree. This mutates the tree after grabbing basis, so go to
177
212
        # the repository.
178
 
        base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
179
 
        tree_z = tree_a.bzrdir.sprout('z').open_workingtree()
 
213
        base_tree = tree_a.branch.repository.revision_tree(
 
214
            tree_a.last_revision())
 
215
        tree_z = tree_a.controldir.sprout('z').open_workingtree()
180
216
        self.build_tree(['a/b/c'])
181
217
        tree_a.add('b/c')
182
218
        tree_a.commit('added c')
184
220
        tree_z.commit('removed b')
185
221
        merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
186
222
        self.assertEqual([
187
 
            conflicts.MissingParent('Created directory', 'b', 'b-id'),
188
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
 
223
            MissingParent('Created directory', 'b', b'b-id'),
 
224
            UnversionedParent('Versioned directory', 'b', b'b-id')],
189
225
            tree_z.conflicts())
190
226
        merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
191
227
                    this_tree=tree_a)
192
228
        self.assertEqual([
193
 
            conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
194
 
            conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
 
229
            DeletingParent('Not deleting', 'b', b'b-id'),
 
230
            UnversionedParent('Versioned directory', 'b', b'b-id')],
195
231
            tree_a.conflicts())
196
232
 
197
233
    def test_nested_merge(self):
 
234
        self.knownFailure(
 
235
            'iter_changes doesn\'t work with changes in nested trees')
198
236
        tree = self.make_branch_and_tree('tree',
199
 
            format='dirstate-with-subtree')
 
237
                                         format='development-subtree')
200
238
        sub_tree = self.make_branch_and_tree('tree/sub-tree',
201
 
            format='dirstate-with-subtree')
202
 
        sub_tree.set_root_id('sub-tree-root')
203
 
        self.build_tree_contents([('tree/sub-tree/file', 'text1')])
 
239
                                             format='development-subtree')
 
240
        sub_tree.set_root_id(b'sub-tree-root')
 
241
        self.build_tree_contents([('tree/sub-tree/file', b'text1')])
204
242
        sub_tree.add('file')
205
243
        sub_tree.commit('foo')
206
244
        tree.add_reference(sub_tree)
207
245
        tree.commit('set text to 1')
208
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
 
246
        tree2 = tree.controldir.sprout('tree2').open_workingtree()
209
247
        # modify the file in the subtree
210
 
        self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
 
248
        self.build_tree_contents([('tree2/sub-tree/file', b'text2')])
211
249
        # and merge the changes from the diverged subtree into the containing
212
250
        # tree
213
251
        tree2.commit('changed file text')
214
252
        tree.merge_from_branch(tree2.branch)
215
 
        self.assertFileEqual('text2', 'tree/sub-tree/file')
 
253
        self.assertFileEqual(b'text2', 'tree/sub-tree/file')
216
254
 
217
255
    def test_merge_with_missing(self):
218
256
        tree_a = self.make_branch_and_tree('tree_a')
219
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
 
257
        self.build_tree_contents([('tree_a/file', b'content_1')])
220
258
        tree_a.add('file')
221
259
        tree_a.commit('commit base')
222
260
        # basis_tree() is only guaranteed to be valid as long as it is actually
223
261
        # the basis tree. This test commits to the tree after grabbing basis,
224
262
        # so we go to the repository.
225
 
        base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
226
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
227
 
        self.build_tree_contents([('tree_a/file', 'content_2')])
 
263
        base_tree = tree_a.branch.repository.revision_tree(
 
264
            tree_a.last_revision())
 
265
        tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
 
266
        self.build_tree_contents([('tree_a/file', b'content_2')])
228
267
        tree_a.commit('commit other')
229
268
        other_tree = tree_a.basis_tree()
230
269
        # 'file' is now missing but isn't altered in any commit in b so no
234
273
 
235
274
    def test_merge_kind_change(self):
236
275
        tree_a = self.make_branch_and_tree('tree_a')
237
 
        self.build_tree_contents([('tree_a/file', 'content_1')])
238
 
        tree_a.add('file', 'file-id')
 
276
        self.build_tree_contents([('tree_a/file', b'content_1')])
 
277
        tree_a.add('file', b'file-id')
239
278
        tree_a.commit('added file')
240
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
 
279
        tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
241
280
        os.unlink('tree_a/file')
242
281
        self.build_tree(['tree_a/file/'])
243
282
        tree_a.commit('changed file to directory')
245
284
        self.assertEqual('directory', file_kind('tree_b/file'))
246
285
        tree_b.revert()
247
286
        self.assertEqual('file', file_kind('tree_b/file'))
248
 
        self.build_tree_contents([('tree_b/file', 'content_2')])
 
287
        self.build_tree_contents([('tree_b/file', b'content_2')])
249
288
        tree_b.commit('content change')
250
289
        tree_b.merge_from_branch(tree_a.branch)
251
290
        self.assertEqual(tree_b.conflicts(),
252
 
                         [conflicts.ContentsConflict('file',
253
 
                          file_id='file-id')])
 
291
                         [ContentsConflict('file', file_id=b'file-id')])
254
292
 
255
293
    def test_merge_type_registry(self):
256
294
        merge_type_option = option.Option.OPTIONS['merge-type']
257
295
        self.assertFalse('merge4' in [x[0] for x in
258
 
                        merge_type_option.iter_switches()])
 
296
                                      merge_type_option.iter_switches()])
259
297
        registry = _mod_merge.get_merge_type_registry()
260
 
        registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
 
298
        registry.register_lazy('merge4', 'breezy.merge', 'Merge4Merger',
261
299
                               'time-travelling merge')
262
300
        self.assertTrue('merge4' in [x[0] for x in
263
 
                        merge_type_option.iter_switches()])
 
301
                                     merge_type_option.iter_switches()])
264
302
        registry.remove('merge4')
265
303
        self.assertFalse('merge4' in [x[0] for x in
266
 
                        merge_type_option.iter_switches()])
 
304
                                      merge_type_option.iter_switches()])
267
305
 
268
306
    def test_merge_other_moves_we_deleted(self):
269
307
        tree_a = self.make_branch_and_tree('A')
271
309
        self.addCleanup(tree_a.unlock)
272
310
        self.build_tree(['A/a'])
273
311
        tree_a.add('a')
274
 
        tree_a.commit('1', rev_id='rev-1')
 
312
        tree_a.commit('1', rev_id=b'rev-1')
275
313
        tree_a.flush()
276
314
        tree_a.rename_one('a', 'b')
277
315
        tree_a.commit('2')
278
 
        bzrdir_b = tree_a.bzrdir.sprout('B', revision_id='rev-1')
 
316
        bzrdir_b = tree_a.controldir.sprout('B', revision_id=b'rev-1')
279
317
        tree_b = bzrdir_b.open_workingtree()
280
318
        tree_b.lock_write()
281
319
        self.addCleanup(tree_b.unlock)
293
331
        tree_a.commit('commit 1')
294
332
        tree_a.add(['file_2'])
295
333
        tree_a.commit('commit 2')
296
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
334
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
297
335
        tree_b.rename_one('file_1', 'renamed')
298
336
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
299
337
        merger.merge_type = _mod_merge.Merge3Merger
307
345
        tree_a.commit('commit 1')
308
346
        tree_a.add(['file_2'])
309
347
        tree_a.commit('commit 2')
310
 
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
348
        tree_b = tree_a.controldir.sprout('b').open_workingtree()
311
349
        tree_b.rename_one('file_1', 'renamed')
312
350
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
313
351
        merger.merge_type = _mod_merge.WeaveMerger
326
364
        'c', but not 'b'.
327
365
        """
328
366
        this_tree = self.make_branch_and_tree('this')
329
 
        self.build_tree_contents([('this/file', "a\n")])
 
367
        self.build_tree_contents([('this/file', b"a\n")])
330
368
        this_tree.add('file')
331
369
        this_tree.commit('rev1')
332
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
333
 
        self.build_tree_contents([('other/file', "a\nb\n")])
334
 
        other_tree.commit('rev2b', rev_id='rev2b')
335
 
        self.build_tree_contents([('other/file', "c\na\nb\n")])
336
 
        other_tree.commit('rev3b', rev_id='rev3b')
 
370
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
371
        self.build_tree_contents([('other/file', b"a\nb\n")])
 
372
        other_tree.commit('rev2b', rev_id=b'rev2b')
 
373
        self.build_tree_contents([('other/file', b"c\na\nb\n")])
 
374
        other_tree.commit('rev3b', rev_id=b'rev3b')
337
375
        this_tree.lock_write()
338
376
        self.addCleanup(this_tree.unlock)
339
377
        return this_tree, other_tree
340
378
 
341
379
    def test_weave_cherrypick(self):
342
380
        this_tree, other_tree = self.prepare_cherrypick()
343
 
        merger = _mod_merge.Merger.from_revision_ids(None,
344
 
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
 
381
        merger = _mod_merge.Merger.from_revision_ids(
 
382
            this_tree, b'rev3b', b'rev2b', other_tree.branch)
345
383
        merger.merge_type = _mod_merge.WeaveMerger
346
384
        merger.do_merge()
347
 
        self.assertFileEqual('c\na\n', 'this/file')
 
385
        self.assertFileEqual(b'c\na\n', 'this/file')
348
386
 
349
387
    def test_weave_cannot_reverse_cherrypick(self):
350
388
        this_tree, other_tree = self.prepare_cherrypick()
351
 
        merger = _mod_merge.Merger.from_revision_ids(None,
352
 
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
 
389
        merger = _mod_merge.Merger.from_revision_ids(
 
390
            this_tree, b'rev2b', b'rev3b', other_tree.branch)
353
391
        merger.merge_type = _mod_merge.WeaveMerger
354
392
        self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
355
393
 
356
394
    def test_merge3_can_reverse_cherrypick(self):
357
395
        this_tree, other_tree = self.prepare_cherrypick()
358
 
        merger = _mod_merge.Merger.from_revision_ids(None,
359
 
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
 
396
        merger = _mod_merge.Merger.from_revision_ids(
 
397
            this_tree, b'rev2b', b'rev3b', other_tree.branch)
360
398
        merger.merge_type = _mod_merge.Merge3Merger
361
399
        merger.do_merge()
362
400
 
363
401
    def test_merge3_will_detect_cherrypick(self):
364
402
        this_tree = self.make_branch_and_tree('this')
365
 
        self.build_tree_contents([('this/file', "a\n")])
 
403
        self.build_tree_contents([('this/file', b"a\n")])
366
404
        this_tree.add('file')
367
405
        this_tree.commit('rev1')
368
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
369
 
        self.build_tree_contents([('other/file', "a\nb\n")])
370
 
        other_tree.commit('rev2b', rev_id='rev2b')
371
 
        self.build_tree_contents([('other/file', "a\nb\nc\n")])
372
 
        other_tree.commit('rev3b', rev_id='rev3b')
 
406
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
407
        self.build_tree_contents([('other/file', b"a\nb\n")])
 
408
        other_tree.commit('rev2b', rev_id=b'rev2b')
 
409
        self.build_tree_contents([('other/file', b"a\nb\nc\n")])
 
410
        other_tree.commit('rev3b', rev_id=b'rev3b')
373
411
        this_tree.lock_write()
374
412
        self.addCleanup(this_tree.unlock)
375
413
 
376
 
        merger = _mod_merge.Merger.from_revision_ids(None,
377
 
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
 
414
        merger = _mod_merge.Merger.from_revision_ids(
 
415
            this_tree, b'rev3b', b'rev2b', other_tree.branch)
378
416
        merger.merge_type = _mod_merge.Merge3Merger
379
417
        merger.do_merge()
380
 
        self.assertFileEqual('a\n'
381
 
                             '<<<<<<< TREE\n'
382
 
                             '=======\n'
383
 
                             'c\n'
384
 
                             '>>>>>>> MERGE-SOURCE\n',
 
418
        self.assertFileEqual(b'a\n'
 
419
                             b'<<<<<<< TREE\n'
 
420
                             b'=======\n'
 
421
                             b'c\n'
 
422
                             b'>>>>>>> MERGE-SOURCE\n',
385
423
                             'this/file')
386
424
 
 
425
    def test_merge_reverse_revision_range(self):
 
426
        tree = self.make_branch_and_tree(".")
 
427
        tree.lock_write()
 
428
        self.addCleanup(tree.unlock)
 
429
        self.build_tree(['a'])
 
430
        tree.add('a')
 
431
        first_rev = tree.commit("added a")
 
432
        merger = _mod_merge.Merger.from_revision_ids(tree,
 
433
                                                     _mod_revision.NULL_REVISION,
 
434
                                                     first_rev)
 
435
        merger.merge_type = _mod_merge.Merge3Merger
 
436
        merger.interesting_files = 'a'
 
437
        conflict_count = merger.do_merge()
 
438
        self.assertEqual(0, conflict_count)
 
439
 
 
440
        self.assertPathDoesNotExist("a")
 
441
        tree.revert()
 
442
        self.assertPathExists("a")
 
443
 
387
444
    def test_make_merger(self):
388
445
        this_tree = self.make_branch_and_tree('this')
389
 
        this_tree.commit('rev1', rev_id='rev1')
390
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
391
 
        this_tree.commit('rev2', rev_id='rev2a')
392
 
        other_tree.commit('rev2', rev_id='rev2b')
 
446
        this_tree.commit('rev1', rev_id=b'rev1')
 
447
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
448
        this_tree.commit('rev2', rev_id=b'rev2a')
 
449
        other_tree.commit('rev2', rev_id=b'rev2b')
393
450
        this_tree.lock_write()
394
451
        self.addCleanup(this_tree.unlock)
395
 
        merger = _mod_merge.Merger.from_revision_ids(None,
396
 
            this_tree, 'rev2b', other_branch=other_tree.branch)
 
452
        merger = _mod_merge.Merger.from_revision_ids(
 
453
            this_tree, b'rev2b', other_branch=other_tree.branch)
397
454
        merger.merge_type = _mod_merge.Merge3Merger
398
455
        tree_merger = merger.make_merger()
399
456
        self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
400
 
        self.assertEqual('rev2b', tree_merger.other_tree.get_revision_id())
401
 
        self.assertEqual('rev1', tree_merger.base_tree.get_revision_id())
 
457
        self.assertEqual(b'rev2b',
 
458
                         tree_merger.other_tree.get_revision_id())
 
459
        self.assertEqual(b'rev1',
 
460
                         tree_merger.base_tree.get_revision_id())
 
461
        self.assertEqual(other_tree.branch, tree_merger.other_branch)
402
462
 
403
463
    def test_make_preview_transform(self):
404
464
        this_tree = self.make_branch_and_tree('this')
405
 
        self.build_tree_contents([('this/file', '1\n')])
406
 
        this_tree.add('file', 'file-id')
407
 
        this_tree.commit('rev1', rev_id='rev1')
408
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
409
 
        self.build_tree_contents([('this/file', '1\n2a\n')])
410
 
        this_tree.commit('rev2', rev_id='rev2a')
411
 
        self.build_tree_contents([('other/file', '2b\n1\n')])
412
 
        other_tree.commit('rev2', rev_id='rev2b')
 
465
        self.build_tree_contents([('this/file', b'1\n')])
 
466
        this_tree.add('file', b'file-id')
 
467
        this_tree.commit('rev1', rev_id=b'rev1')
 
468
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
469
        self.build_tree_contents([('this/file', b'1\n2a\n')])
 
470
        this_tree.commit('rev2', rev_id=b'rev2a')
 
471
        self.build_tree_contents([('other/file', b'2b\n1\n')])
 
472
        other_tree.commit('rev2', rev_id=b'rev2b')
413
473
        this_tree.lock_write()
414
474
        self.addCleanup(this_tree.unlock)
415
 
        merger = _mod_merge.Merger.from_revision_ids(None,
416
 
            this_tree, 'rev2b', other_branch=other_tree.branch)
 
475
        merger = _mod_merge.Merger.from_revision_ids(
 
476
            this_tree, b'rev2b', other_branch=other_tree.branch)
417
477
        merger.merge_type = _mod_merge.Merge3Merger
418
478
        tree_merger = merger.make_merger()
419
 
        tt = tree_merger.make_preview_transform()
420
 
        self.addCleanup(tt.finalize)
421
 
        preview_tree = tt.get_preview_tree()
422
 
        tree_file = this_tree.get_file('file-id')
423
 
        try:
424
 
            self.assertEqual('1\n2a\n', tree_file.read())
425
 
        finally:
426
 
            tree_file.close()
427
 
        preview_file = preview_tree.get_file('file-id')
428
 
        try:
429
 
            self.assertEqual('2b\n1\n2a\n', preview_file.read())
430
 
        finally:
431
 
            preview_file.close()
 
479
        with tree_merger.make_preview_transform() as tt:
 
480
            preview_tree = tt.get_preview_tree()
 
481
            with this_tree.get_file('file') as tree_file:
 
482
                self.assertEqual(b'1\n2a\n', tree_file.read())
 
483
            with preview_tree.get_file('file') as preview_file:
 
484
                self.assertEqual(b'2b\n1\n2a\n', preview_file.read())
432
485
 
433
486
    def test_do_merge(self):
434
487
        this_tree = self.make_branch_and_tree('this')
435
 
        self.build_tree_contents([('this/file', '1\n')])
436
 
        this_tree.add('file', 'file-id')
437
 
        this_tree.commit('rev1', rev_id='rev1')
438
 
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
439
 
        self.build_tree_contents([('this/file', '1\n2a\n')])
440
 
        this_tree.commit('rev2', rev_id='rev2a')
441
 
        self.build_tree_contents([('other/file', '2b\n1\n')])
442
 
        other_tree.commit('rev2', rev_id='rev2b')
 
488
        self.build_tree_contents([('this/file', b'1\n')])
 
489
        this_tree.add('file', b'file-id')
 
490
        this_tree.commit('rev1', rev_id=b'rev1')
 
491
        other_tree = this_tree.controldir.sprout('other').open_workingtree()
 
492
        self.build_tree_contents([('this/file', b'1\n2a\n')])
 
493
        this_tree.commit('rev2', rev_id=b'rev2a')
 
494
        self.build_tree_contents([('other/file', b'2b\n1\n')])
 
495
        other_tree.commit('rev2', rev_id=b'rev2b')
443
496
        this_tree.lock_write()
444
497
        self.addCleanup(this_tree.unlock)
445
 
        merger = _mod_merge.Merger.from_revision_ids(None,
446
 
            this_tree, 'rev2b', other_branch=other_tree.branch)
 
498
        merger = _mod_merge.Merger.from_revision_ids(
 
499
            this_tree, b'rev2b', other_branch=other_tree.branch)
447
500
        merger.merge_type = _mod_merge.Merge3Merger
448
501
        tree_merger = merger.make_merger()
449
502
        tt = tree_merger.do_merge()
450
 
        tree_file = this_tree.get_file('file-id')
451
 
        try:
452
 
            self.assertEqual('2b\n1\n2a\n', tree_file.read())
453
 
        finally:
454
 
            tree_file.close()
 
503
        with this_tree.get_file('file') as tree_file:
 
504
            self.assertEqual(b'2b\n1\n2a\n', tree_file.read())
 
505
 
 
506
    def test_merge_require_tree_root(self):
 
507
        tree = self.make_branch_and_tree(".")
 
508
        tree.lock_write()
 
509
        self.addCleanup(tree.unlock)
 
510
        self.build_tree(['a'])
 
511
        tree.add('a')
 
512
        first_rev = tree.commit("added a")
 
513
        old_root_id = tree.path2id('')
 
514
        merger = _mod_merge.Merger.from_revision_ids(tree,
 
515
                                                     _mod_revision.NULL_REVISION,
 
516
                                                     first_rev)
 
517
        merger.merge_type = _mod_merge.Merge3Merger
 
518
        conflict_count = merger.do_merge()
 
519
        self.assertEqual(0, conflict_count)
 
520
        self.assertEqual({''}, set(tree.all_versioned_paths()))
 
521
        tree.set_parent_ids([])
455
522
 
456
523
    def test_merge_add_into_deleted_root(self):
457
524
        # Yes, people actually do this.  And report bugs if it breaks.
458
525
        source = self.make_branch_and_tree('source', format='rich-root-pack')
459
526
        self.build_tree(['source/foo/'])
460
 
        source.add('foo', 'foo-id')
 
527
        source.add('foo', b'foo-id')
461
528
        source.commit('Add foo')
462
 
        target = source.bzrdir.sprout('target').open_workingtree()
463
 
        subtree = target.extract('foo-id')
 
529
        target = source.controldir.sprout('target').open_workingtree()
 
530
        subtree = target.extract('foo')
464
531
        subtree.commit('Delete root')
465
532
        self.build_tree(['source/bar'])
466
 
        source.add('bar', 'bar-id')
 
533
        source.add('bar', b'bar-id')
467
534
        source.commit('Add bar')
468
535
        subtree.merge_from_branch(source.branch)
469
536
 
476
543
        self.build_tree(['target/bla'])
477
544
        target.add('bla')
478
545
        target.commit('Add bla')
479
 
        nested = source.bzrdir.sprout('target/subtree').open_workingtree()
 
546
        nested = source.controldir.sprout('target/subtree').open_workingtree()
480
547
        target.subsume(nested)
481
548
        target.commit('Join nested')
482
549
        self.build_tree(['source/bar'])
489
556
class TestPlanMerge(TestCaseWithMemoryTransport):
490
557
 
491
558
    def setUp(self):
492
 
        TestCaseWithMemoryTransport.setUp(self)
 
559
        super(TestPlanMerge, self).setUp()
493
560
        mapper = versionedfile.PrefixMapper()
494
561
        factory = knit.make_file_factory(True, mapper)
495
562
        self.vf = factory(self.get_transport())
496
 
        self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root')
 
563
        self.plan_merge_vf = versionedfile._PlanMergeVersionedFile(b'root')
497
564
        self.plan_merge_vf.fallback_versionedfiles.append(self.vf)
498
565
 
499
566
    def add_version(self, key, parents, text):
500
 
        self.vf.add_lines(key, parents, [c+'\n' for c in text])
 
567
        self.vf.add_lines(
 
568
            key, parents, [bytes([c]) + b'\n' for c in bytearray(text)])
501
569
 
502
570
    def add_rev(self, prefix, revision_id, parents, text):
503
571
        self.add_version((prefix, revision_id), [(prefix, p) for p in parents],
505
573
 
506
574
    def add_uncommitted_version(self, key, parents, text):
507
575
        self.plan_merge_vf.add_lines(key, parents,
508
 
                                     [c+'\n' for c in text])
 
576
                                     [bytes([c]) + b'\n' for c in bytearray(text)])
509
577
 
510
578
    def setup_plan_merge(self):
511
 
        self.add_rev('root', 'A', [], 'abc')
512
 
        self.add_rev('root', 'B', ['A'], 'acehg')
513
 
        self.add_rev('root', 'C', ['A'], 'fabg')
514
 
        return _PlanMerge('B', 'C', self.plan_merge_vf, ('root',))
 
579
        self.add_rev(b'root', b'A', [], b'abc')
 
580
        self.add_rev(b'root', b'B', [b'A'], b'acehg')
 
581
        self.add_rev(b'root', b'C', [b'A'], b'fabg')
 
582
        return _PlanMerge(b'B', b'C', self.plan_merge_vf, (b'root',))
515
583
 
516
584
    def setup_plan_merge_uncommitted(self):
517
 
        self.add_version(('root', 'A'), [], 'abc')
518
 
        self.add_uncommitted_version(('root', 'B:'), [('root', 'A')], 'acehg')
519
 
        self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
520
 
        return _PlanMerge('B:', 'C:', self.plan_merge_vf, ('root',))
 
585
        self.add_version((b'root', b'A'), [], b'abc')
 
586
        self.add_uncommitted_version(
 
587
            (b'root', b'B:'), [(b'root', b'A')], b'acehg')
 
588
        self.add_uncommitted_version(
 
589
            (b'root', b'C:'), [(b'root', b'A')], b'fabg')
 
590
        return _PlanMerge(b'B:', b'C:', self.plan_merge_vf, (b'root',))
521
591
 
522
592
    def test_base_from_plan(self):
523
593
        self.setup_plan_merge()
524
 
        plan = self.plan_merge_vf.plan_merge('B', 'C')
 
594
        plan = self.plan_merge_vf.plan_merge(b'B', b'C')
525
595
        pwm = versionedfile.PlanWeaveMerge(plan)
526
 
        self.assertEqual(['a\n', 'b\n', 'c\n'], pwm.base_from_plan())
 
596
        self.assertEqual([b'a\n', b'b\n', b'c\n'], pwm.base_from_plan())
527
597
 
528
598
    def test_unique_lines(self):
529
599
        plan = self.setup_plan_merge()
530
600
        self.assertEqual(plan._unique_lines(
531
 
            plan._get_matching_blocks('B', 'C')),
 
601
            plan._get_matching_blocks(b'B', b'C')),
532
602
            ([1, 2, 3], [0, 2]))
533
603
 
534
604
    def test_plan_merge(self):
535
605
        self.setup_plan_merge()
536
 
        plan = self.plan_merge_vf.plan_merge('B', 'C')
 
606
        plan = self.plan_merge_vf.plan_merge(b'B', b'C')
537
607
        self.assertEqual([
538
 
                          ('new-b', 'f\n'),
539
 
                          ('unchanged', 'a\n'),
540
 
                          ('killed-a', 'b\n'),
541
 
                          ('killed-b', 'c\n'),
542
 
                          ('new-a', 'e\n'),
543
 
                          ('new-a', 'h\n'),
544
 
                          ('new-a', 'g\n'),
545
 
                          ('new-b', 'g\n')],
546
 
                         list(plan))
 
608
            ('new-b', b'f\n'),
 
609
            ('unchanged', b'a\n'),
 
610
            ('killed-a', b'b\n'),
 
611
            ('killed-b', b'c\n'),
 
612
            ('new-a', b'e\n'),
 
613
            ('new-a', b'h\n'),
 
614
            ('new-a', b'g\n'),
 
615
            ('new-b', b'g\n')],
 
616
            list(plan))
547
617
 
548
618
    def test_plan_merge_cherrypick(self):
549
 
        self.add_rev('root', 'A', [], 'abc')
550
 
        self.add_rev('root', 'B', ['A'], 'abcde')
551
 
        self.add_rev('root', 'C', ['A'], 'abcefg')
552
 
        self.add_rev('root', 'D', ['A', 'B', 'C'], 'abcdegh')
553
 
        my_plan = _PlanMerge('B', 'D', self.plan_merge_vf, ('root',))
 
619
        self.add_rev(b'root', b'A', [], b'abc')
 
620
        self.add_rev(b'root', b'B', [b'A'], b'abcde')
 
621
        self.add_rev(b'root', b'C', [b'A'], b'abcefg')
 
622
        self.add_rev(b'root', b'D', [b'A', b'B', b'C'], b'abcdegh')
 
623
        my_plan = _PlanMerge(b'B', b'D', self.plan_merge_vf, (b'root',))
554
624
        # We shortcut when one text supersedes the other in the per-file graph.
555
625
        # We don't actually need to compare the texts at this point.
556
626
        self.assertEqual([
557
 
                          ('new-b', 'a\n'),
558
 
                          ('new-b', 'b\n'),
559
 
                          ('new-b', 'c\n'),
560
 
                          ('new-b', 'd\n'),
561
 
                          ('new-b', 'e\n'),
562
 
                          ('new-b', 'g\n'),
563
 
                          ('new-b', 'h\n')],
564
 
                          list(my_plan.plan_merge()))
 
627
            ('new-b', b'a\n'),
 
628
            ('new-b', b'b\n'),
 
629
            ('new-b', b'c\n'),
 
630
            ('new-b', b'd\n'),
 
631
            ('new-b', b'e\n'),
 
632
            ('new-b', b'g\n'),
 
633
            ('new-b', b'h\n')],
 
634
            list(my_plan.plan_merge()))
565
635
 
566
636
    def test_plan_merge_no_common_ancestor(self):
567
 
        self.add_rev('root', 'A', [], 'abc')
568
 
        self.add_rev('root', 'B', [], 'xyz')
569
 
        my_plan = _PlanMerge('A', 'B', self.plan_merge_vf, ('root',))
 
637
        self.add_rev(b'root', b'A', [], b'abc')
 
638
        self.add_rev(b'root', b'B', [], b'xyz')
 
639
        my_plan = _PlanMerge(b'A', b'B', self.plan_merge_vf, (b'root',))
570
640
        self.assertEqual([
571
 
                          ('new-a', 'a\n'),
572
 
                          ('new-a', 'b\n'),
573
 
                          ('new-a', 'c\n'),
574
 
                          ('new-b', 'x\n'),
575
 
                          ('new-b', 'y\n'),
576
 
                          ('new-b', 'z\n')],
577
 
                          list(my_plan.plan_merge()))
 
641
            ('new-a', b'a\n'),
 
642
            ('new-a', b'b\n'),
 
643
            ('new-a', b'c\n'),
 
644
            ('new-b', b'x\n'),
 
645
            ('new-b', b'y\n'),
 
646
            ('new-b', b'z\n')],
 
647
            list(my_plan.plan_merge()))
578
648
 
579
649
    def test_plan_merge_tail_ancestors(self):
580
650
        # The graph looks like this:
596
666
        # are already present in E
597
667
 
598
668
        # Introduce the base text
599
 
        self.add_rev('root', 'A', [], 'abc')
 
669
        self.add_rev(b'root', b'A', [], b'abc')
600
670
        # Introduces a new line B
601
 
        self.add_rev('root', 'B', ['A'], 'aBbc')
 
671
        self.add_rev(b'root', b'B', [b'A'], b'aBbc')
602
672
        # Introduces a new line C
603
 
        self.add_rev('root', 'C', ['A'], 'abCc')
 
673
        self.add_rev(b'root', b'C', [b'A'], b'abCc')
604
674
        # Introduce new line D
605
 
        self.add_rev('root', 'D', ['B'], 'DaBbc')
 
675
        self.add_rev(b'root', b'D', [b'B'], b'DaBbc')
606
676
        # Merges B and C by just incorporating both
607
 
        self.add_rev('root', 'E', ['B', 'C'], 'aBbCc')
 
677
        self.add_rev(b'root', b'E', [b'B', b'C'], b'aBbCc')
608
678
        # Introduce new line F
609
 
        self.add_rev('root', 'F', ['C'], 'abCcF')
 
679
        self.add_rev(b'root', b'F', [b'C'], b'abCcF')
610
680
        # Merge D & E by just combining the texts
611
 
        self.add_rev('root', 'G', ['D', 'E'], 'DaBbCc')
 
681
        self.add_rev(b'root', b'G', [b'D', b'E'], b'DaBbCc')
612
682
        # Merge F & E by just combining the texts
613
 
        self.add_rev('root', 'H', ['F', 'E'], 'aBbCcF')
 
683
        self.add_rev(b'root', b'H', [b'F', b'E'], b'aBbCcF')
614
684
        # Merge G & H by just combining texts
615
 
        self.add_rev('root', 'I', ['G', 'H'], 'DaBbCcF')
 
685
        self.add_rev(b'root', b'I', [b'G', b'H'], b'DaBbCcF')
616
686
        # Merge G & H but supersede an old line in B
617
 
        self.add_rev('root', 'J', ['H', 'G'], 'DaJbCcF')
618
 
        plan = self.plan_merge_vf.plan_merge('I', 'J')
 
687
        self.add_rev(b'root', b'J', [b'H', b'G'], b'DaJbCcF')
 
688
        plan = self.plan_merge_vf.plan_merge(b'I', b'J')
619
689
        self.assertEqual([
620
 
                          ('unchanged', 'D\n'),
621
 
                          ('unchanged', 'a\n'),
622
 
                          ('killed-b', 'B\n'),
623
 
                          ('new-b', 'J\n'),
624
 
                          ('unchanged', 'b\n'),
625
 
                          ('unchanged', 'C\n'),
626
 
                          ('unchanged', 'c\n'),
627
 
                          ('unchanged', 'F\n')],
628
 
                         list(plan))
 
690
            ('unchanged', b'D\n'),
 
691
            ('unchanged', b'a\n'),
 
692
            ('killed-b', b'B\n'),
 
693
            ('new-b', b'J\n'),
 
694
            ('unchanged', b'b\n'),
 
695
            ('unchanged', b'C\n'),
 
696
            ('unchanged', b'c\n'),
 
697
            ('unchanged', b'F\n')],
 
698
            list(plan))
629
699
 
630
700
    def test_plan_merge_tail_triple_ancestors(self):
631
701
        # The graph looks like this:
645
715
        # a third LCA that doesn't add new lines, but will trigger our more
646
716
        # involved ancestry logic
647
717
 
648
 
        self.add_rev('root', 'A', [], 'abc')
649
 
        self.add_rev('root', 'B', ['A'], 'aBbc')
650
 
        self.add_rev('root', 'C', ['A'], 'abCc')
651
 
        self.add_rev('root', 'D', ['B'], 'DaBbc')
652
 
        self.add_rev('root', 'E', ['B', 'C'], 'aBbCc')
653
 
        self.add_rev('root', 'F', ['C'], 'abCcF')
654
 
        self.add_rev('root', 'G', ['D', 'E'], 'DaBbCc')
655
 
        self.add_rev('root', 'H', ['F', 'E'], 'aBbCcF')
656
 
        self.add_rev('root', 'Q', ['E'], 'aBbCc')
657
 
        self.add_rev('root', 'I', ['G', 'Q', 'H'], 'DaBbCcF')
 
718
        self.add_rev(b'root', b'A', [], b'abc')
 
719
        self.add_rev(b'root', b'B', [b'A'], b'aBbc')
 
720
        self.add_rev(b'root', b'C', [b'A'], b'abCc')
 
721
        self.add_rev(b'root', b'D', [b'B'], b'DaBbc')
 
722
        self.add_rev(b'root', b'E', [b'B', b'C'], b'aBbCc')
 
723
        self.add_rev(b'root', b'F', [b'C'], b'abCcF')
 
724
        self.add_rev(b'root', b'G', [b'D', b'E'], b'DaBbCc')
 
725
        self.add_rev(b'root', b'H', [b'F', b'E'], b'aBbCcF')
 
726
        self.add_rev(b'root', b'Q', [b'E'], b'aBbCc')
 
727
        self.add_rev(b'root', b'I', [b'G', b'Q', b'H'], b'DaBbCcF')
658
728
        # Merge G & H but supersede an old line in B
659
 
        self.add_rev('root', 'J', ['H', 'Q', 'G'], 'DaJbCcF')
660
 
        plan = self.plan_merge_vf.plan_merge('I', 'J')
 
729
        self.add_rev(b'root', b'J', [b'H', b'Q', b'G'], b'DaJbCcF')
 
730
        plan = self.plan_merge_vf.plan_merge(b'I', b'J')
661
731
        self.assertEqual([
662
 
                          ('unchanged', 'D\n'),
663
 
                          ('unchanged', 'a\n'),
664
 
                          ('killed-b', 'B\n'),
665
 
                          ('new-b', 'J\n'),
666
 
                          ('unchanged', 'b\n'),
667
 
                          ('unchanged', 'C\n'),
668
 
                          ('unchanged', 'c\n'),
669
 
                          ('unchanged', 'F\n')],
670
 
                         list(plan))
 
732
            ('unchanged', b'D\n'),
 
733
            ('unchanged', b'a\n'),
 
734
            ('killed-b', b'B\n'),
 
735
            ('new-b', b'J\n'),
 
736
            ('unchanged', b'b\n'),
 
737
            ('unchanged', b'C\n'),
 
738
            ('unchanged', b'c\n'),
 
739
            ('unchanged', b'F\n')],
 
740
            list(plan))
671
741
 
672
742
    def test_plan_merge_2_tail_triple_ancestors(self):
673
743
        # The graph looks like this:
685
755
        # This is meant to test after hitting a 3-way LCA, and multiple tail
686
756
        # ancestors (only have NULL_REVISION in common)
687
757
 
688
 
        self.add_rev('root', 'A', [], 'abc')
689
 
        self.add_rev('root', 'B', [], 'def')
690
 
        self.add_rev('root', 'D', ['A'], 'Dabc')
691
 
        self.add_rev('root', 'E', ['A', 'B'], 'abcdef')
692
 
        self.add_rev('root', 'F', ['B'], 'defF')
693
 
        self.add_rev('root', 'G', ['D', 'E'], 'Dabcdef')
694
 
        self.add_rev('root', 'H', ['F', 'E'], 'abcdefF')
695
 
        self.add_rev('root', 'Q', ['E'], 'abcdef')
696
 
        self.add_rev('root', 'I', ['G', 'Q', 'H'], 'DabcdefF')
 
758
        self.add_rev(b'root', b'A', [], b'abc')
 
759
        self.add_rev(b'root', b'B', [], b'def')
 
760
        self.add_rev(b'root', b'D', [b'A'], b'Dabc')
 
761
        self.add_rev(b'root', b'E', [b'A', b'B'], b'abcdef')
 
762
        self.add_rev(b'root', b'F', [b'B'], b'defF')
 
763
        self.add_rev(b'root', b'G', [b'D', b'E'], b'Dabcdef')
 
764
        self.add_rev(b'root', b'H', [b'F', b'E'], b'abcdefF')
 
765
        self.add_rev(b'root', b'Q', [b'E'], b'abcdef')
 
766
        self.add_rev(b'root', b'I', [b'G', b'Q', b'H'], b'DabcdefF')
697
767
        # Merge G & H but supersede an old line in B
698
 
        self.add_rev('root', 'J', ['H', 'Q', 'G'], 'DabcdJfF')
699
 
        plan = self.plan_merge_vf.plan_merge('I', 'J')
 
768
        self.add_rev(b'root', b'J', [b'H', b'Q', b'G'], b'DabcdJfF')
 
769
        plan = self.plan_merge_vf.plan_merge(b'I', b'J')
700
770
        self.assertEqual([
701
 
                          ('unchanged', 'D\n'),
702
 
                          ('unchanged', 'a\n'),
703
 
                          ('unchanged', 'b\n'),
704
 
                          ('unchanged', 'c\n'),
705
 
                          ('unchanged', 'd\n'),
706
 
                          ('killed-b', 'e\n'),
707
 
                          ('new-b', 'J\n'),
708
 
                          ('unchanged', 'f\n'),
709
 
                          ('unchanged', 'F\n')],
710
 
                         list(plan))
 
771
            ('unchanged', b'D\n'),
 
772
            ('unchanged', b'a\n'),
 
773
            ('unchanged', b'b\n'),
 
774
            ('unchanged', b'c\n'),
 
775
            ('unchanged', b'd\n'),
 
776
            ('killed-b', b'e\n'),
 
777
            ('new-b', b'J\n'),
 
778
            ('unchanged', b'f\n'),
 
779
            ('unchanged', b'F\n')],
 
780
            list(plan))
711
781
 
712
782
    def test_plan_merge_uncommitted_files(self):
713
783
        self.setup_plan_merge_uncommitted()
714
 
        plan = self.plan_merge_vf.plan_merge('B:', 'C:')
 
784
        plan = self.plan_merge_vf.plan_merge(b'B:', b'C:')
715
785
        self.assertEqual([
716
 
                          ('new-b', 'f\n'),
717
 
                          ('unchanged', 'a\n'),
718
 
                          ('killed-a', 'b\n'),
719
 
                          ('killed-b', 'c\n'),
720
 
                          ('new-a', 'e\n'),
721
 
                          ('new-a', 'h\n'),
722
 
                          ('new-a', 'g\n'),
723
 
                          ('new-b', 'g\n')],
724
 
                         list(plan))
 
786
            ('new-b', b'f\n'),
 
787
            ('unchanged', b'a\n'),
 
788
            ('killed-a', b'b\n'),
 
789
            ('killed-b', b'c\n'),
 
790
            ('new-a', b'e\n'),
 
791
            ('new-a', b'h\n'),
 
792
            ('new-a', b'g\n'),
 
793
            ('new-b', b'g\n')],
 
794
            list(plan))
725
795
 
726
796
    def test_plan_merge_insert_order(self):
727
797
        """Weave merges are sensitive to the order of insertion.
731
801
        same ordering, then the lines match the parents, if they don't only
732
802
        *some* of the lines match.
733
803
        """
734
 
        self.add_rev('root', 'A', [], 'abcdef')
735
 
        self.add_rev('root', 'B', ['A'], 'abwxcdef')
736
 
        self.add_rev('root', 'C', ['A'], 'abyzcdef')
 
804
        self.add_rev(b'root', b'A', [], b'abcdef')
 
805
        self.add_rev(b'root', b'B', [b'A'], b'abwxcdef')
 
806
        self.add_rev(b'root', b'C', [b'A'], b'abyzcdef')
737
807
        # Merge, and resolve the conflict by adding *both* sets of lines
738
808
        # If we get the ordering wrong, these will look like new lines in D,
739
809
        # rather than carried over from B, C
740
 
        self.add_rev('root', 'D', ['B', 'C'],
741
 
                         'abwxyzcdef')
 
810
        self.add_rev(b'root', b'D', [b'B', b'C'],
 
811
                     b'abwxyzcdef')
742
812
        # Supersede the lines in B and delete the lines in C, which will
743
813
        # conflict if they are treated as being in D
744
 
        self.add_rev('root', 'E', ['C', 'B'],
745
 
                         'abnocdef')
 
814
        self.add_rev(b'root', b'E', [b'C', b'B'],
 
815
                     b'abnocdef')
746
816
        # Same thing for the lines in C
747
 
        self.add_rev('root', 'F', ['C'], 'abpqcdef')
748
 
        plan = self.plan_merge_vf.plan_merge('D', 'E')
 
817
        self.add_rev(b'root', b'F', [b'C'], b'abpqcdef')
 
818
        plan = self.plan_merge_vf.plan_merge(b'D', b'E')
749
819
        self.assertEqual([
750
 
                          ('unchanged', 'a\n'),
751
 
                          ('unchanged', 'b\n'),
752
 
                          ('killed-b', 'w\n'),
753
 
                          ('killed-b', 'x\n'),
754
 
                          ('killed-b', 'y\n'),
755
 
                          ('killed-b', 'z\n'),
756
 
                          ('new-b', 'n\n'),
757
 
                          ('new-b', 'o\n'),
758
 
                          ('unchanged', 'c\n'),
759
 
                          ('unchanged', 'd\n'),
760
 
                          ('unchanged', 'e\n'),
761
 
                          ('unchanged', 'f\n')],
762
 
                         list(plan))
763
 
        plan = self.plan_merge_vf.plan_merge('E', 'D')
 
820
            ('unchanged', b'a\n'),
 
821
            ('unchanged', b'b\n'),
 
822
            ('killed-b', b'w\n'),
 
823
            ('killed-b', b'x\n'),
 
824
            ('killed-b', b'y\n'),
 
825
            ('killed-b', b'z\n'),
 
826
            ('new-b', b'n\n'),
 
827
            ('new-b', b'o\n'),
 
828
            ('unchanged', b'c\n'),
 
829
            ('unchanged', b'd\n'),
 
830
            ('unchanged', b'e\n'),
 
831
            ('unchanged', b'f\n')],
 
832
            list(plan))
 
833
        plan = self.plan_merge_vf.plan_merge(b'E', b'D')
764
834
        # Going in the opposite direction shows the effect of the opposite plan
765
835
        self.assertEqual([
766
 
                          ('unchanged', 'a\n'),
767
 
                          ('unchanged', 'b\n'),
768
 
                          ('new-b', 'w\n'),
769
 
                          ('new-b', 'x\n'),
770
 
                          ('killed-a', 'y\n'),
771
 
                          ('killed-a', 'z\n'),
772
 
                          ('killed-both', 'w\n'),
773
 
                          ('killed-both', 'x\n'),
774
 
                          ('new-a', 'n\n'),
775
 
                          ('new-a', 'o\n'),
776
 
                          ('unchanged', 'c\n'),
777
 
                          ('unchanged', 'd\n'),
778
 
                          ('unchanged', 'e\n'),
779
 
                          ('unchanged', 'f\n')],
780
 
                         list(plan))
 
836
            ('unchanged', b'a\n'),
 
837
            ('unchanged', b'b\n'),
 
838
            ('new-b', b'w\n'),
 
839
            ('new-b', b'x\n'),
 
840
            ('killed-a', b'y\n'),
 
841
            ('killed-a', b'z\n'),
 
842
            ('killed-both', b'w\n'),
 
843
            ('killed-both', b'x\n'),
 
844
            ('new-a', b'n\n'),
 
845
            ('new-a', b'o\n'),
 
846
            ('unchanged', b'c\n'),
 
847
            ('unchanged', b'd\n'),
 
848
            ('unchanged', b'e\n'),
 
849
            ('unchanged', b'f\n')],
 
850
            list(plan))
781
851
 
782
852
    def test_plan_merge_criss_cross(self):
783
853
        # This is specificly trying to trigger problems when using limited
804
874
        #   'foo', it should appear as superseding the value in F (since it
805
875
        #   came from B), rather than conflict because of the resolution during
806
876
        #   C & D.
807
 
        self.add_rev('root', 'XX', [], 'qrs')
808
 
        self.add_rev('root', 'A', ['XX'], 'abcdef')
809
 
        self.add_rev('root', 'B', ['A'], 'axcdef')
810
 
        self.add_rev('root', 'C', ['B'], 'axcdefg')
811
 
        self.add_rev('root', 'D', ['B'], 'haxcdef')
812
 
        self.add_rev('root', 'E', ['A'], 'abcdyf')
 
877
        self.add_rev(b'root', b'XX', [], b'qrs')
 
878
        self.add_rev(b'root', b'A', [b'XX'], b'abcdef')
 
879
        self.add_rev(b'root', b'B', [b'A'], b'axcdef')
 
880
        self.add_rev(b'root', b'C', [b'B'], b'axcdefg')
 
881
        self.add_rev(b'root', b'D', [b'B'], b'haxcdef')
 
882
        self.add_rev(b'root', b'E', [b'A'], b'abcdyf')
813
883
        # Simple combining of all texts
814
 
        self.add_rev('root', 'F', ['C', 'D', 'E'], 'haxcdyfg')
 
884
        self.add_rev(b'root', b'F', [b'C', b'D', b'E'], b'haxcdyfg')
815
885
        # combine and supersede 'x'
816
 
        self.add_rev('root', 'G', ['C', 'D', 'E'], 'hazcdyfg')
817
 
        plan = self.plan_merge_vf.plan_merge('F', 'G')
 
886
        self.add_rev(b'root', b'G', [b'C', b'D', b'E'], b'hazcdyfg')
 
887
        plan = self.plan_merge_vf.plan_merge(b'F', b'G')
818
888
        self.assertEqual([
819
 
                          ('unchanged', 'h\n'),
820
 
                          ('unchanged', 'a\n'),
821
 
                          ('killed-base', 'b\n'),
822
 
                          ('killed-b', 'x\n'),
823
 
                          ('new-b', 'z\n'),
824
 
                          ('unchanged', 'c\n'),
825
 
                          ('unchanged', 'd\n'),
826
 
                          ('killed-base', 'e\n'),
827
 
                          ('unchanged', 'y\n'),
828
 
                          ('unchanged', 'f\n'),
829
 
                          ('unchanged', 'g\n')],
830
 
                         list(plan))
831
 
        plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
 
889
            ('unchanged', b'h\n'),
 
890
            ('unchanged', b'a\n'),
 
891
            ('killed-base', b'b\n'),
 
892
            ('killed-b', b'x\n'),
 
893
            ('new-b', b'z\n'),
 
894
            ('unchanged', b'c\n'),
 
895
            ('unchanged', b'd\n'),
 
896
            ('killed-base', b'e\n'),
 
897
            ('unchanged', b'y\n'),
 
898
            ('unchanged', b'f\n'),
 
899
            ('unchanged', b'g\n')],
 
900
            list(plan))
 
901
        plan = self.plan_merge_vf.plan_lca_merge(b'F', b'G')
832
902
        # This is one of the main differences between plan_merge and
833
903
        # plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
834
904
        # because 'x' was not present in one of the bases. However, in this
835
905
        # case it is spurious because 'x' does not exist in the global base A.
836
906
        self.assertEqual([
837
 
                          ('unchanged', 'h\n'),
838
 
                          ('unchanged', 'a\n'),
839
 
                          ('conflicted-a', 'x\n'),
840
 
                          ('new-b', 'z\n'),
841
 
                          ('unchanged', 'c\n'),
842
 
                          ('unchanged', 'd\n'),
843
 
                          ('unchanged', 'y\n'),
844
 
                          ('unchanged', 'f\n'),
845
 
                          ('unchanged', 'g\n')],
846
 
                         list(plan))
 
907
            ('unchanged', b'h\n'),
 
908
            ('unchanged', b'a\n'),
 
909
            ('conflicted-a', b'x\n'),
 
910
            ('new-b', b'z\n'),
 
911
            ('unchanged', b'c\n'),
 
912
            ('unchanged', b'd\n'),
 
913
            ('unchanged', b'y\n'),
 
914
            ('unchanged', b'f\n'),
 
915
            ('unchanged', b'g\n')],
 
916
            list(plan))
847
917
 
848
918
    def test_criss_cross_flip_flop(self):
849
919
        # This is specificly trying to trigger problems when using limited
851
921
        #       XX      unused ancestor, should not show up in the weave
852
922
        #       |
853
923
        #       A       Unique LCA
854
 
        #      / \  
 
924
        #      / \
855
925
        #     B   C     B & C both introduce a new line
856
 
        #     |\ /|  
857
 
        #     | X |  
858
 
        #     |/ \| 
 
926
        #     |\ /|
 
927
        #     | X |
 
928
        #     |/ \|
859
929
        #     D   E     B & C are both merged, so both are common ancestors
860
930
        #               In the process of merging, both sides order the new
861
931
        #               lines differently
862
932
        #
863
 
        self.add_rev('root', 'XX', [], 'qrs')
864
 
        self.add_rev('root', 'A', ['XX'], 'abcdef')
865
 
        self.add_rev('root', 'B', ['A'], 'abcdgef')
866
 
        self.add_rev('root', 'C', ['A'], 'abcdhef')
867
 
        self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
868
 
        self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
869
 
        plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
 
933
        self.add_rev(b'root', b'XX', [], b'qrs')
 
934
        self.add_rev(b'root', b'A', [b'XX'], b'abcdef')
 
935
        self.add_rev(b'root', b'B', [b'A'], b'abcdgef')
 
936
        self.add_rev(b'root', b'C', [b'A'], b'abcdhef')
 
937
        self.add_rev(b'root', b'D', [b'B', b'C'], b'abcdghef')
 
938
        self.add_rev(b'root', b'E', [b'C', b'B'], b'abcdhgef')
 
939
        plan = list(self.plan_merge_vf.plan_merge(b'D', b'E'))
870
940
        self.assertEqual([
871
 
                          ('unchanged', 'a\n'),
872
 
                          ('unchanged', 'b\n'),
873
 
                          ('unchanged', 'c\n'),
874
 
                          ('unchanged', 'd\n'),
875
 
                          ('new-b', 'h\n'),
876
 
                          ('unchanged', 'g\n'),
877
 
                          ('killed-b', 'h\n'),
878
 
                          ('unchanged', 'e\n'),
879
 
                          ('unchanged', 'f\n'),
880
 
                         ], plan)
 
941
            ('unchanged', b'a\n'),
 
942
            ('unchanged', b'b\n'),
 
943
            ('unchanged', b'c\n'),
 
944
            ('unchanged', b'd\n'),
 
945
            ('new-b', b'h\n'),
 
946
            ('unchanged', b'g\n'),
 
947
            ('killed-b', b'h\n'),
 
948
            ('unchanged', b'e\n'),
 
949
            ('unchanged', b'f\n'),
 
950
            ], plan)
881
951
        pwm = versionedfile.PlanWeaveMerge(plan)
882
 
        self.assertEqualDiff('\n'.join('abcdghef') + '\n',
883
 
                             ''.join(pwm.base_from_plan()))
 
952
        self.assertEqualDiff(b'a\nb\nc\nd\ng\nh\ne\nf\n',
 
953
                             b''.join(pwm.base_from_plan()))
884
954
        # Reversing the order reverses the merge plan, and final order of 'hg'
885
955
        # => 'gh'
886
 
        plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
 
956
        plan = list(self.plan_merge_vf.plan_merge(b'E', b'D'))
887
957
        self.assertEqual([
888
 
                          ('unchanged', 'a\n'),
889
 
                          ('unchanged', 'b\n'),
890
 
                          ('unchanged', 'c\n'),
891
 
                          ('unchanged', 'd\n'),
892
 
                          ('new-b', 'g\n'),
893
 
                          ('unchanged', 'h\n'),
894
 
                          ('killed-b', 'g\n'),
895
 
                          ('unchanged', 'e\n'),
896
 
                          ('unchanged', 'f\n'),
897
 
                         ], plan)
 
958
            ('unchanged', b'a\n'),
 
959
            ('unchanged', b'b\n'),
 
960
            ('unchanged', b'c\n'),
 
961
            ('unchanged', b'd\n'),
 
962
            ('new-b', b'g\n'),
 
963
            ('unchanged', b'h\n'),
 
964
            ('killed-b', b'g\n'),
 
965
            ('unchanged', b'e\n'),
 
966
            ('unchanged', b'f\n'),
 
967
            ], plan)
898
968
        pwm = versionedfile.PlanWeaveMerge(plan)
899
 
        self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
900
 
                             ''.join(pwm.base_from_plan()))
 
969
        self.assertEqualDiff(b'a\nb\nc\nd\nh\ng\ne\nf\n',
 
970
                             b''.join(pwm.base_from_plan()))
901
971
        # This is where lca differs, in that it (fairly correctly) determines
902
972
        # that there is a conflict because both sides resolved the merge
903
973
        # differently
904
 
        plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
 
974
        plan = list(self.plan_merge_vf.plan_lca_merge(b'D', b'E'))
905
975
        self.assertEqual([
906
 
                          ('unchanged', 'a\n'),
907
 
                          ('unchanged', 'b\n'),
908
 
                          ('unchanged', 'c\n'),
909
 
                          ('unchanged', 'd\n'),
910
 
                          ('conflicted-b', 'h\n'),
911
 
                          ('unchanged', 'g\n'),
912
 
                          ('conflicted-a', 'h\n'),
913
 
                          ('unchanged', 'e\n'),
914
 
                          ('unchanged', 'f\n'),
915
 
                         ], plan)
 
976
            ('unchanged', b'a\n'),
 
977
            ('unchanged', b'b\n'),
 
978
            ('unchanged', b'c\n'),
 
979
            ('unchanged', b'd\n'),
 
980
            ('conflicted-b', b'h\n'),
 
981
            ('unchanged', b'g\n'),
 
982
            ('conflicted-a', b'h\n'),
 
983
            ('unchanged', b'e\n'),
 
984
            ('unchanged', b'f\n'),
 
985
            ], plan)
916
986
        pwm = versionedfile.PlanWeaveMerge(plan)
917
 
        self.assertEqualDiff('\n'.join('abcdgef') + '\n',
918
 
                             ''.join(pwm.base_from_plan()))
 
987
        self.assertEqualDiff(b'a\nb\nc\nd\ng\ne\nf\n',
 
988
                             b''.join(pwm.base_from_plan()))
919
989
        # Reversing it changes what line is doubled, but still gives a
920
990
        # double-conflict
921
 
        plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
 
991
        plan = list(self.plan_merge_vf.plan_lca_merge(b'E', b'D'))
922
992
        self.assertEqual([
923
 
                          ('unchanged', 'a\n'),
924
 
                          ('unchanged', 'b\n'),
925
 
                          ('unchanged', 'c\n'),
926
 
                          ('unchanged', 'd\n'),
927
 
                          ('conflicted-b', 'g\n'),
928
 
                          ('unchanged', 'h\n'),
929
 
                          ('conflicted-a', 'g\n'),
930
 
                          ('unchanged', 'e\n'),
931
 
                          ('unchanged', 'f\n'),
932
 
                         ], plan)
 
993
            ('unchanged', b'a\n'),
 
994
            ('unchanged', b'b\n'),
 
995
            ('unchanged', b'c\n'),
 
996
            ('unchanged', b'd\n'),
 
997
            ('conflicted-b', b'g\n'),
 
998
            ('unchanged', b'h\n'),
 
999
            ('conflicted-a', b'g\n'),
 
1000
            ('unchanged', b'e\n'),
 
1001
            ('unchanged', b'f\n'),
 
1002
            ], plan)
933
1003
        pwm = versionedfile.PlanWeaveMerge(plan)
934
 
        self.assertEqualDiff('\n'.join('abcdhef') + '\n',
935
 
                             ''.join(pwm.base_from_plan()))
 
1004
        self.assertEqualDiff(b'a\nb\nc\nd\nh\ne\nf\n',
 
1005
                             b''.join(pwm.base_from_plan()))
936
1006
 
937
1007
    def assertRemoveExternalReferences(self, filtered_parent_map,
938
1008
                                       child_map, tails, parent_map):
953
1023
    def test__remove_external_references(self):
954
1024
        # First, nothing to remove
955
1025
        self.assertRemoveExternalReferences({3: [2], 2: [1], 1: []},
956
 
            {1: [2], 2: [3], 3: []}, [1], {3: [2], 2: [1], 1: []})
 
1026
                                            {1: [2], 2: [3], 3: []}, [1], {3: [2], 2: [1], 1: []})
957
1027
        # The reverse direction
958
1028
        self.assertRemoveExternalReferences({1: [2], 2: [3], 3: []},
959
 
            {3: [2], 2: [1], 1: []}, [3], {1: [2], 2: [3], 3: []})
 
1029
                                            {3: [2], 2: [1], 1: []}, [3], {1: [2], 2: [3], 3: []})
960
1030
        # Extra references
961
1031
        self.assertRemoveExternalReferences({3: [2], 2: [1], 1: []},
962
 
            {1: [2], 2: [3], 3: []}, [1], {3: [2, 4], 2: [1, 5], 1: [6]})
 
1032
                                            {1: [2], 2: [3], 3: []}, [1], {3: [2, 4], 2: [1, 5], 1: [6]})
963
1033
        # Multiple tails
964
1034
        self.assertRemoveExternalReferences(
965
1035
            {4: [2, 3], 3: [], 2: [1], 1: []},
975
1045
 
976
1046
    def assertPruneTails(self, pruned_map, tails, parent_map):
977
1047
        child_map = {}
978
 
        for key, parent_keys in parent_map.iteritems():
 
1048
        for key, parent_keys in parent_map.items():
979
1049
            child_map.setdefault(key, [])
980
1050
            for pkey in parent_keys:
981
1051
                child_map.setdefault(pkey, []).append(key)
996
1066
        self.assertPruneTails({1: []}, [5],
997
1067
                              {1: [], 2: [3, 4], 3: [5], 4: [5], 5: []})
998
1068
        # Prune a partial chain
999
 
        self.assertPruneTails({1: [6], 6:[]}, [5],
 
1069
        self.assertPruneTails({1: [6], 6: []}, [5],
1000
1070
                              {1: [2, 6], 2: [3, 4], 3: [5], 4: [5], 5: [],
1001
1071
                               6: []})
1002
1072
        # Prune a chain with multiple tips, that pulls out intermediates
1003
 
        self.assertPruneTails({1:[3], 3:[]}, [4, 5],
1004
 
                              {1: [2, 3], 2: [4, 5], 3: [], 4:[], 5:[]})
1005
 
        self.assertPruneTails({1:[3], 3:[]}, [5, 4],
1006
 
                              {1: [2, 3], 2: [4, 5], 3: [], 4:[], 5:[]})
 
1073
        self.assertPruneTails({1: [3], 3: []}, [4, 5],
 
1074
                              {1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []})
 
1075
        self.assertPruneTails({1: [3], 3: []}, [5, 4],
 
1076
                              {1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []})
1007
1077
 
1008
1078
    def test_subtract_plans(self):
1009
1079
        old_plan = [
1010
 
        ('unchanged', 'a\n'),
1011
 
        ('new-a', 'b\n'),
1012
 
        ('killed-a', 'c\n'),
1013
 
        ('new-b', 'd\n'),
1014
 
        ('new-b', 'e\n'),
1015
 
        ('killed-b', 'f\n'),
1016
 
        ('killed-b', 'g\n'),
 
1080
            ('unchanged', b'a\n'),
 
1081
            ('new-a', b'b\n'),
 
1082
            ('killed-a', b'c\n'),
 
1083
            ('new-b', b'd\n'),
 
1084
            ('new-b', b'e\n'),
 
1085
            ('killed-b', b'f\n'),
 
1086
            ('killed-b', b'g\n'),
1017
1087
        ]
1018
1088
        new_plan = [
1019
 
        ('unchanged', 'a\n'),
1020
 
        ('new-a', 'b\n'),
1021
 
        ('killed-a', 'c\n'),
1022
 
        ('new-b', 'd\n'),
1023
 
        ('new-b', 'h\n'),
1024
 
        ('killed-b', 'f\n'),
1025
 
        ('killed-b', 'i\n'),
 
1089
            ('unchanged', b'a\n'),
 
1090
            ('new-a', b'b\n'),
 
1091
            ('killed-a', b'c\n'),
 
1092
            ('new-b', b'd\n'),
 
1093
            ('new-b', b'h\n'),
 
1094
            ('killed-b', b'f\n'),
 
1095
            ('killed-b', b'i\n'),
1026
1096
        ]
1027
1097
        subtracted_plan = [
1028
 
        ('unchanged', 'a\n'),
1029
 
        ('new-a', 'b\n'),
1030
 
        ('killed-a', 'c\n'),
1031
 
        ('new-b', 'h\n'),
1032
 
        ('unchanged', 'f\n'),
1033
 
        ('killed-b', 'i\n'),
 
1098
            ('unchanged', b'a\n'),
 
1099
            ('new-a', b'b\n'),
 
1100
            ('killed-a', b'c\n'),
 
1101
            ('new-b', b'h\n'),
 
1102
            ('unchanged', b'f\n'),
 
1103
            ('killed-b', b'i\n'),
1034
1104
        ]
1035
1105
        self.assertEqual(subtracted_plan,
1036
 
            list(_PlanMerge._subtract_plans(old_plan, new_plan)))
 
1106
                         list(_PlanMerge._subtract_plans(old_plan, new_plan)))
1037
1107
 
1038
1108
    def setup_merge_with_base(self):
1039
 
        self.add_rev('root', 'COMMON', [], 'abc')
1040
 
        self.add_rev('root', 'THIS', ['COMMON'], 'abcd')
1041
 
        self.add_rev('root', 'BASE', ['COMMON'], 'eabc')
1042
 
        self.add_rev('root', 'OTHER', ['BASE'], 'eafb')
 
1109
        self.add_rev(b'root', b'COMMON', [], b'abc')
 
1110
        self.add_rev(b'root', b'THIS', [b'COMMON'], b'abcd')
 
1111
        self.add_rev(b'root', b'BASE', [b'COMMON'], b'eabc')
 
1112
        self.add_rev(b'root', b'OTHER', [b'BASE'], b'eafb')
1043
1113
 
1044
1114
    def test_plan_merge_with_base(self):
1045
1115
        self.setup_merge_with_base()
1046
 
        plan = self.plan_merge_vf.plan_merge('THIS', 'OTHER', 'BASE')
1047
 
        self.assertEqual([('unchanged', 'a\n'),
1048
 
                          ('new-b', 'f\n'),
1049
 
                          ('unchanged', 'b\n'),
1050
 
                          ('killed-b', 'c\n'),
1051
 
                          ('new-a', 'd\n')
1052
 
                         ], list(plan))
 
1116
        plan = self.plan_merge_vf.plan_merge(b'THIS', b'OTHER', b'BASE')
 
1117
        self.assertEqual([('unchanged', b'a\n'),
 
1118
                          ('new-b', b'f\n'),
 
1119
                          ('unchanged', b'b\n'),
 
1120
                          ('killed-b', b'c\n'),
 
1121
                          ('new-a', b'd\n')
 
1122
                          ], list(plan))
1053
1123
 
1054
1124
    def test_plan_lca_merge(self):
1055
1125
        self.setup_plan_merge()
1056
 
        plan = self.plan_merge_vf.plan_lca_merge('B', 'C')
 
1126
        plan = self.plan_merge_vf.plan_lca_merge(b'B', b'C')
1057
1127
        self.assertEqual([
1058
 
                          ('new-b', 'f\n'),
1059
 
                          ('unchanged', 'a\n'),
1060
 
                          ('killed-b', 'c\n'),
1061
 
                          ('new-a', 'e\n'),
1062
 
                          ('new-a', 'h\n'),
1063
 
                          ('killed-a', 'b\n'),
1064
 
                          ('unchanged', 'g\n')],
1065
 
                         list(plan))
 
1128
            ('new-b', b'f\n'),
 
1129
            ('unchanged', b'a\n'),
 
1130
            ('killed-b', b'c\n'),
 
1131
            ('new-a', b'e\n'),
 
1132
            ('new-a', b'h\n'),
 
1133
            ('killed-a', b'b\n'),
 
1134
            ('unchanged', b'g\n')],
 
1135
            list(plan))
1066
1136
 
1067
1137
    def test_plan_lca_merge_uncommitted_files(self):
1068
1138
        self.setup_plan_merge_uncommitted()
1069
 
        plan = self.plan_merge_vf.plan_lca_merge('B:', 'C:')
 
1139
        plan = self.plan_merge_vf.plan_lca_merge(b'B:', b'C:')
1070
1140
        self.assertEqual([
1071
 
                          ('new-b', 'f\n'),
1072
 
                          ('unchanged', 'a\n'),
1073
 
                          ('killed-b', 'c\n'),
1074
 
                          ('new-a', 'e\n'),
1075
 
                          ('new-a', 'h\n'),
1076
 
                          ('killed-a', 'b\n'),
1077
 
                          ('unchanged', 'g\n')],
1078
 
                         list(plan))
 
1141
            ('new-b', b'f\n'),
 
1142
            ('unchanged', b'a\n'),
 
1143
            ('killed-b', b'c\n'),
 
1144
            ('new-a', b'e\n'),
 
1145
            ('new-a', b'h\n'),
 
1146
            ('killed-a', b'b\n'),
 
1147
            ('unchanged', b'g\n')],
 
1148
            list(plan))
1079
1149
 
1080
1150
    def test_plan_lca_merge_with_base(self):
1081
1151
        self.setup_merge_with_base()
1082
 
        plan = self.plan_merge_vf.plan_lca_merge('THIS', 'OTHER', 'BASE')
1083
 
        self.assertEqual([('unchanged', 'a\n'),
1084
 
                          ('new-b', 'f\n'),
1085
 
                          ('unchanged', 'b\n'),
1086
 
                          ('killed-b', 'c\n'),
1087
 
                          ('new-a', 'd\n')
1088
 
                         ], list(plan))
 
1152
        plan = self.plan_merge_vf.plan_lca_merge(b'THIS', b'OTHER', b'BASE')
 
1153
        self.assertEqual([('unchanged', b'a\n'),
 
1154
                          ('new-b', b'f\n'),
 
1155
                          ('unchanged', b'b\n'),
 
1156
                          ('killed-b', b'c\n'),
 
1157
                          ('new-a', b'd\n')
 
1158
                          ], list(plan))
1089
1159
 
1090
1160
    def test_plan_lca_merge_with_criss_cross(self):
1091
 
        self.add_version(('root', 'ROOT'), [], 'abc')
 
1161
        self.add_version((b'root', b'ROOT'), [], b'abc')
1092
1162
        # each side makes a change
1093
 
        self.add_version(('root', 'REV1'), [('root', 'ROOT')], 'abcd')
1094
 
        self.add_version(('root', 'REV2'), [('root', 'ROOT')], 'abce')
 
1163
        self.add_version((b'root', b'REV1'), [(b'root', b'ROOT')], b'abcd')
 
1164
        self.add_version((b'root', b'REV2'), [(b'root', b'ROOT')], b'abce')
1095
1165
        # both sides merge, discarding others' changes
1096
 
        self.add_version(('root', 'LCA1'),
1097
 
            [('root', 'REV1'), ('root', 'REV2')], 'abcd')
1098
 
        self.add_version(('root', 'LCA2'),
1099
 
            [('root', 'REV1'), ('root', 'REV2')], 'fabce')
1100
 
        plan = self.plan_merge_vf.plan_lca_merge('LCA1', 'LCA2')
1101
 
        self.assertEqual([('new-b', 'f\n'),
1102
 
                          ('unchanged', 'a\n'),
1103
 
                          ('unchanged', 'b\n'),
1104
 
                          ('unchanged', 'c\n'),
1105
 
                          ('conflicted-a', 'd\n'),
1106
 
                          ('conflicted-b', 'e\n'),
1107
 
                         ], list(plan))
 
1166
        self.add_version((b'root', b'LCA1'),
 
1167
                         [(b'root', b'REV1'), (b'root', b'REV2')], b'abcd')
 
1168
        self.add_version((b'root', b'LCA2'),
 
1169
                         [(b'root', b'REV1'), (b'root', b'REV2')], b'fabce')
 
1170
        plan = self.plan_merge_vf.plan_lca_merge(b'LCA1', b'LCA2')
 
1171
        self.assertEqual([('new-b', b'f\n'),
 
1172
                          ('unchanged', b'a\n'),
 
1173
                          ('unchanged', b'b\n'),
 
1174
                          ('unchanged', b'c\n'),
 
1175
                          ('conflicted-a', b'd\n'),
 
1176
                          ('conflicted-b', b'e\n'),
 
1177
                          ], list(plan))
1108
1178
 
1109
1179
    def test_plan_lca_merge_with_null(self):
1110
 
        self.add_version(('root', 'A'), [], 'ab')
1111
 
        self.add_version(('root', 'B'), [], 'bc')
1112
 
        plan = self.plan_merge_vf.plan_lca_merge('A', 'B')
1113
 
        self.assertEqual([('new-a', 'a\n'),
1114
 
                          ('unchanged', 'b\n'),
1115
 
                          ('new-b', 'c\n'),
1116
 
                         ], list(plan))
 
1180
        self.add_version((b'root', b'A'), [], b'ab')
 
1181
        self.add_version((b'root', b'B'), [], b'bc')
 
1182
        plan = self.plan_merge_vf.plan_lca_merge(b'A', b'B')
 
1183
        self.assertEqual([('new-a', b'a\n'),
 
1184
                          ('unchanged', b'b\n'),
 
1185
                          ('new-b', b'c\n'),
 
1186
                          ], list(plan))
1117
1187
 
1118
1188
    def test_plan_merge_with_delete_and_change(self):
1119
 
        self.add_rev('root', 'C', [], 'a')
1120
 
        self.add_rev('root', 'A', ['C'], 'b')
1121
 
        self.add_rev('root', 'B', ['C'], '')
1122
 
        plan = self.plan_merge_vf.plan_merge('A', 'B')
1123
 
        self.assertEqual([('killed-both', 'a\n'),
1124
 
                          ('new-a', 'b\n'),
1125
 
                         ], list(plan))
 
1189
        self.add_rev(b'root', b'C', [], b'a')
 
1190
        self.add_rev(b'root', b'A', [b'C'], b'b')
 
1191
        self.add_rev(b'root', b'B', [b'C'], b'')
 
1192
        plan = self.plan_merge_vf.plan_merge(b'A', b'B')
 
1193
        self.assertEqual([('killed-both', b'a\n'),
 
1194
                          ('new-a', b'b\n'),
 
1195
                          ], list(plan))
1126
1196
 
1127
1197
    def test_plan_merge_with_move_and_change(self):
1128
 
        self.add_rev('root', 'C', [], 'abcd')
1129
 
        self.add_rev('root', 'A', ['C'], 'acbd')
1130
 
        self.add_rev('root', 'B', ['C'], 'aBcd')
1131
 
        plan = self.plan_merge_vf.plan_merge('A', 'B')
1132
 
        self.assertEqual([('unchanged', 'a\n'),
1133
 
                          ('new-a', 'c\n'),
1134
 
                          ('killed-b', 'b\n'),
1135
 
                          ('new-b', 'B\n'),
1136
 
                          ('killed-a', 'c\n'),
1137
 
                          ('unchanged', 'd\n'),
1138
 
                         ], list(plan))
 
1198
        self.add_rev(b'root', b'C', [], b'abcd')
 
1199
        self.add_rev(b'root', b'A', [b'C'], b'acbd')
 
1200
        self.add_rev(b'root', b'B', [b'C'], b'aBcd')
 
1201
        plan = self.plan_merge_vf.plan_merge(b'A', b'B')
 
1202
        self.assertEqual([('unchanged', b'a\n'),
 
1203
                          ('new-a', b'c\n'),
 
1204
                          ('killed-b', b'b\n'),
 
1205
                          ('new-b', b'B\n'),
 
1206
                          ('killed-a', b'c\n'),
 
1207
                          ('unchanged', b'd\n'),
 
1208
                          ], list(plan))
1139
1209
 
1140
1210
 
1141
1211
class LoggingMerger(object):
1171
1241
        #  B C
1172
1242
        #
1173
1243
        builder = self.get_builder()
1174
 
        builder.build_snapshot('A-id', None,
1175
 
            [('add', ('', None, 'directory', None))])
1176
 
        builder.build_snapshot('C-id', ['A-id'], [])
1177
 
        builder.build_snapshot('B-id', ['A-id'], [])
 
1244
        builder.build_snapshot(None,
 
1245
                               [('add', ('', None, 'directory', None))],
 
1246
                               revision_id=b'A-id')
 
1247
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1248
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1178
1249
        return builder
1179
1250
 
1180
1251
    def setup_criss_cross_graph(self):
1188
1259
        # |X|
1189
1260
        # D E
1190
1261
        builder = self.setup_simple_graph()
1191
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1192
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
 
1262
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1263
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1193
1264
        return builder
1194
1265
 
1195
 
    def make_Merger(self, builder, other_revision_id,
1196
 
                    interesting_files=None, interesting_ids=None):
 
1266
    def make_Merger(self, builder, other_revision_id, interesting_files=None):
1197
1267
        """Make a Merger object from a branch builder"""
1198
1268
        mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1199
1269
        mem_tree.lock_write()
1200
1270
        self.addCleanup(mem_tree.unlock)
1201
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
1271
        merger = _mod_merge.Merger.from_revision_ids(
1202
1272
            mem_tree, other_revision_id)
1203
1273
        merger.set_interesting_files(interesting_files)
1204
 
        # It seems there is no matching function for set_interesting_ids
1205
 
        merger.interesting_ids = interesting_ids
1206
1274
        merger.merge_type = _mod_merge.Merge3Merger
1207
1275
        return merger
1208
1276
 
1210
1278
class TestMergerInMemory(TestMergerBase):
1211
1279
 
1212
1280
    def test_cache_trees_with_revision_ids_None(self):
1213
 
        merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
 
1281
        merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1214
1282
        original_cache = dict(merger._cached_trees)
1215
1283
        merger.cache_trees_with_revision_ids([None])
1216
1284
        self.assertEqual(original_cache, merger._cached_trees)
1217
1285
 
1218
1286
    def test_cache_trees_with_revision_ids_no_revision_id(self):
1219
 
        merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
 
1287
        merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1220
1288
        original_cache = dict(merger._cached_trees)
1221
1289
        tree = self.make_branch_and_memory_tree('tree')
1222
1290
        merger.cache_trees_with_revision_ids([tree])
1223
1291
        self.assertEqual(original_cache, merger._cached_trees)
1224
1292
 
1225
1293
    def test_cache_trees_with_revision_ids_having_revision_id(self):
1226
 
        merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
 
1294
        merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1227
1295
        original_cache = dict(merger._cached_trees)
1228
 
        tree = merger.this_branch.repository.revision_tree('B-id')
1229
 
        original_cache['B-id'] = tree
 
1296
        tree = merger.this_branch.repository.revision_tree(b'B-id')
 
1297
        original_cache[b'B-id'] = tree
1230
1298
        merger.cache_trees_with_revision_ids([tree])
1231
1299
        self.assertEqual(original_cache, merger._cached_trees)
1232
1300
 
1233
1301
    def test_find_base(self):
1234
 
        merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1235
 
        self.assertEqual('A-id', merger.base_rev_id)
 
1302
        merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
 
1303
        self.assertEqual(b'A-id', merger.base_rev_id)
1236
1304
        self.assertFalse(merger._is_criss_cross)
1237
1305
        self.assertIs(None, merger._lca_trees)
1238
1306
 
1239
1307
    def test_find_base_criss_cross(self):
1240
1308
        builder = self.setup_criss_cross_graph()
1241
 
        merger = self.make_Merger(builder, 'E-id')
1242
 
        self.assertEqual('A-id', merger.base_rev_id)
 
1309
        merger = self.make_Merger(builder, b'E-id')
 
1310
        self.assertEqual(b'A-id', merger.base_rev_id)
1243
1311
        self.assertTrue(merger._is_criss_cross)
1244
 
        self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1245
 
                                            for t in merger._lca_trees])
 
1312
        self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
 
1313
                                              for t in merger._lca_trees])
1246
1314
        # If we swap the order, we should get a different lca order
1247
 
        builder.build_snapshot('F-id', ['E-id'], [])
1248
 
        merger = self.make_Merger(builder, 'D-id')
1249
 
        self.assertEqual(['C-id', 'B-id'], [t.get_revision_id()
1250
 
                                            for t in merger._lca_trees])
 
1315
        builder.build_snapshot([b'E-id'], [], revision_id=b'F-id')
 
1316
        merger = self.make_Merger(builder, b'D-id')
 
1317
        self.assertEqual([b'C-id', b'B-id'], [t.get_revision_id()
 
1318
                                              for t in merger._lca_trees])
1251
1319
 
1252
1320
    def test_find_base_triple_criss_cross(self):
1253
1321
        #       A-.
1260
1328
        #    \|   |/
1261
1329
        #     G   H
1262
1330
        builder = self.setup_criss_cross_graph()
1263
 
        builder.build_snapshot('F-id', ['A-id'], [])
1264
 
        builder.build_snapshot('H-id', ['E-id', 'F-id'], [])
1265
 
        builder.build_snapshot('G-id', ['D-id', 'F-id'], [])
1266
 
        merger = self.make_Merger(builder, 'H-id')
1267
 
        self.assertEqual(['B-id', 'C-id', 'F-id'],
 
1331
        builder.build_snapshot([b'A-id'], [], revision_id=b'F-id')
 
1332
        builder.build_snapshot([b'E-id', b'F-id'], [], revision_id=b'H-id')
 
1333
        builder.build_snapshot([b'D-id', b'F-id'], [], revision_id=b'G-id')
 
1334
        merger = self.make_Merger(builder, b'H-id')
 
1335
        self.assertEqual([b'B-id', b'C-id', b'F-id'],
1268
1336
                         [t.get_revision_id() for t in merger._lca_trees])
1269
1337
 
 
1338
    def test_find_base_new_root_criss_cross(self):
 
1339
        # A   B
 
1340
        # |\ /|
 
1341
        # | X |
 
1342
        # |/ \|
 
1343
        # C   D
 
1344
        builder = self.get_builder()
 
1345
        builder.build_snapshot(None,
 
1346
                               [('add', ('', None, 'directory', None))],
 
1347
                               revision_id=b'A-id')
 
1348
        builder.build_snapshot([],
 
1349
                               [('add', ('', None, 'directory', None))],
 
1350
                               revision_id=b'B-id')
 
1351
        builder.build_snapshot([b'A-id', b'B-id'], [], revision_id=b'D-id')
 
1352
        builder.build_snapshot([b'A-id', b'B-id'], [], revision_id=b'C-id')
 
1353
        merger = self.make_Merger(builder, b'D-id')
 
1354
        self.assertEqual(b'A-id', merger.base_rev_id)
 
1355
        self.assertTrue(merger._is_criss_cross)
 
1356
        self.assertEqual([b'A-id', b'B-id'], [t.get_revision_id()
 
1357
                                              for t in merger._lca_trees])
 
1358
 
1270
1359
    def test_no_criss_cross_passed_to_merge_type(self):
1271
1360
        class LCATreesMerger(LoggingMerger):
1272
1361
            supports_lca_trees = True
1273
1362
 
1274
 
        merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
 
1363
        merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1275
1364
        merger.merge_type = LCATreesMerger
1276
1365
        merge_obj = merger.make_merger()
1277
1366
        self.assertIsInstance(merge_obj, LCATreesMerger)
1278
1367
        self.assertFalse('lca_trees' in merge_obj.kwargs)
1279
1368
 
1280
1369
    def test_criss_cross_passed_to_merge_type(self):
1281
 
        merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
 
1370
        merger = self.make_Merger(self.setup_criss_cross_graph(), b'E-id')
1282
1371
        merger.merge_type = _mod_merge.Merge3Merger
1283
1372
        merge_obj = merger.make_merger()
1284
 
        self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1285
 
                                            for t in merger._lca_trees])
 
1373
        self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
 
1374
                                              for t in merger._lca_trees])
1286
1375
 
1287
1376
    def test_criss_cross_not_supported_merge_type(self):
1288
 
        merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
 
1377
        merger = self.make_Merger(self.setup_criss_cross_graph(), b'E-id')
1289
1378
        # We explicitly do not define supports_lca_trees
1290
1379
        merger.merge_type = LoggingMerger
1291
1380
        merge_obj = merger.make_merger()
1296
1385
        class UnsupportedLCATreesMerger(LoggingMerger):
1297
1386
            supports_lca_trees = False
1298
1387
 
1299
 
        merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
 
1388
        merger = self.make_Merger(self.setup_criss_cross_graph(), b'E-id')
1300
1389
        merger.merge_type = UnsupportedLCATreesMerger
1301
1390
        merge_obj = merger.make_merger()
1302
1391
        self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1306
1395
class TestMergerEntriesLCA(TestMergerBase):
1307
1396
 
1308
1397
    def make_merge_obj(self, builder, other_revision_id,
1309
 
                       interesting_files=None, interesting_ids=None):
 
1398
                       interesting_files=None):
1310
1399
        merger = self.make_Merger(builder, other_revision_id,
1311
 
            interesting_files=interesting_files,
1312
 
            interesting_ids=interesting_ids)
 
1400
                                  interesting_files=interesting_files)
1313
1401
        return merger.make_merger()
1314
1402
 
1315
1403
    def test_simple(self):
1316
1404
        builder = self.get_builder()
1317
 
        builder.build_snapshot('A-id', None,
1318
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1319
 
             ('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1320
 
        builder.build_snapshot('C-id', ['A-id'],
1321
 
            [('modify', ('a-id', 'a\nb\nC\nc\n'))])
1322
 
        builder.build_snapshot('B-id', ['A-id'],
1323
 
            [('modify', ('a-id', 'a\nB\nb\nc\n'))])
1324
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1325
 
            [('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1326
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1327
 
            [('modify', ('a-id', 'a\nB\nb\nC\nc\n'))])
1328
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1405
        builder.build_snapshot(None,
 
1406
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1407
                                ('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1408
                               revision_id=b'A-id')
 
1409
        builder.build_snapshot([b'A-id'],
 
1410
                               [('modify', ('a', b'a\nb\nC\nc\n'))],
 
1411
                               revision_id=b'C-id')
 
1412
        builder.build_snapshot([b'A-id'],
 
1413
                               [('modify', ('a', b'a\nB\nb\nc\n'))],
 
1414
                               revision_id=b'B-id')
 
1415
        builder.build_snapshot([b'C-id', b'B-id'],
 
1416
                               [('modify', ('a', b'a\nB\nb\nC\nc\nE\n'))],
 
1417
                               revision_id=b'E-id')
 
1418
        builder.build_snapshot([b'B-id', b'C-id'],
 
1419
                               [('modify', ('a', b'a\nB\nb\nC\nc\n'))],
 
1420
                               revision_id=b'D-id', )
 
1421
        merge_obj = self.make_merge_obj(builder, b'E-id')
1329
1422
 
1330
 
        self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1331
 
                                            for t in merge_obj._lca_trees])
1332
 
        self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
 
1423
        self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
 
1424
                                              for t in merge_obj._lca_trees])
 
1425
        self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1333
1426
        entries = list(merge_obj._entries_lca())
1334
1427
 
1335
1428
        # (file_id, changed, parents, names, executable)
1336
1429
        # BASE, lca1, lca2, OTHER, THIS
1337
 
        root_id = 'a-root-id'
1338
 
        self.assertEqual([('a-id', True,
 
1430
        root_id = b'a-root-id'
 
1431
        self.assertEqual([(b'a-id', True,
 
1432
                           ((u'a', [u'a', u'a']), u'a', u'a'),
1339
1433
                           ((root_id, [root_id, root_id]), root_id, root_id),
1340
1434
                           ((u'a', [u'a', u'a']), u'a', u'a'),
1341
 
                           ((False, [False, False]), False, False)),
1342
 
                         ], entries)
 
1435
                           ((False, [False, False]), False, False),
 
1436
                           False),
 
1437
                          ], entries)
1343
1438
 
1344
1439
    def test_not_in_base(self):
1345
1440
        # LCAs all have the same last-modified revision for the file, as do
1354
1449
        #            G modifies 'bar'
1355
1450
 
1356
1451
        builder = self.get_builder()
1357
 
        builder.build_snapshot('A-id', None,
1358
 
            [('add', (u'', 'a-root-id', 'directory', None))])
1359
 
        builder.build_snapshot('B-id', ['A-id'],
1360
 
            [('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1361
 
        builder.build_snapshot('C-id', ['A-id'],
1362
 
            [('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1363
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1364
 
            [('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1365
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1366
 
            [('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1367
 
        builder.build_snapshot('G-id', ['E-id', 'D-id'],
1368
 
            [('modify', (u'bar-id', 'd\ne\nf\nG\n'))])
1369
 
        builder.build_snapshot('F-id', ['D-id', 'E-id'], [])
1370
 
        merge_obj = self.make_merge_obj(builder, 'G-id')
 
1452
        builder.build_snapshot(None,
 
1453
                               [('add', (u'', b'a-root-id', 'directory', None))],
 
1454
                               revision_id=b'A-id')
 
1455
        builder.build_snapshot([b'A-id'],
 
1456
                               [('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
 
1457
                               revision_id=b'B-id')
 
1458
        builder.build_snapshot([b'A-id'],
 
1459
                               [('add', (u'bar', b'bar-id', 'file', b'd\ne\nf\n'))],
 
1460
                               revision_id=b'C-id')
 
1461
        builder.build_snapshot([b'B-id', b'C-id'],
 
1462
                               [('add', (u'bar', b'bar-id', 'file', b'd\ne\nf\n'))],
 
1463
                               revision_id=b'D-id')
 
1464
        builder.build_snapshot([b'C-id', b'B-id'],
 
1465
                               [('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
 
1466
                               revision_id=b'E-id')
 
1467
        builder.build_snapshot([b'E-id', b'D-id'],
 
1468
                               [('modify', (u'bar', b'd\ne\nf\nG\n'))],
 
1469
                               revision_id=b'G-id')
 
1470
        builder.build_snapshot([b'D-id', b'E-id'], [], revision_id=b'F-id')
 
1471
        merge_obj = self.make_merge_obj(builder, b'G-id')
1371
1472
 
1372
 
        self.assertEqual(['D-id', 'E-id'], [t.get_revision_id()
1373
 
                                            for t in merge_obj._lca_trees])
1374
 
        self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
 
1473
        self.assertEqual([b'D-id', b'E-id'], [t.get_revision_id()
 
1474
                                              for t in merge_obj._lca_trees])
 
1475
        self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1375
1476
        entries = list(merge_obj._entries_lca())
1376
 
        root_id = 'a-root-id'
1377
 
        self.assertEqual([('bar-id', True,
 
1477
        root_id = b'a-root-id'
 
1478
        self.assertEqual([(b'bar-id', True,
 
1479
                           ((None, [u'bar', u'bar']), u'bar', u'bar'),
1378
1480
                           ((None, [root_id, root_id]), root_id, root_id),
1379
1481
                           ((None, [u'bar', u'bar']), u'bar', u'bar'),
1380
 
                           ((None, [False, False]), False, False)),
1381
 
                         ], entries)
 
1482
                           ((None, [False, False]), False, False),
 
1483
                           False),
 
1484
                          ], entries)
1382
1485
 
1383
1486
    def test_not_in_this(self):
1384
1487
        builder = self.get_builder()
1385
 
        builder.build_snapshot('A-id', None,
1386
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1387
 
             ('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1388
 
        builder.build_snapshot('B-id', ['A-id'],
1389
 
            [('modify', ('a-id', 'a\nB\nb\nc\n'))])
1390
 
        builder.build_snapshot('C-id', ['A-id'],
1391
 
            [('modify', ('a-id', 'a\nb\nC\nc\n'))])
1392
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1393
 
            [('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1394
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1395
 
            [('unversion', 'a-id')])
1396
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1488
        builder.build_snapshot(None,
 
1489
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1490
                                ('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1491
                               revision_id=b'A-id')
 
1492
        builder.build_snapshot([b'A-id'],
 
1493
                               [('modify', ('a', b'a\nB\nb\nc\n'))],
 
1494
                               revision_id=b'B-id')
 
1495
        builder.build_snapshot([b'A-id'],
 
1496
                               [('modify', ('a', b'a\nb\nC\nc\n'))],
 
1497
                               revision_id=b'C-id')
 
1498
        builder.build_snapshot([b'C-id', b'B-id'],
 
1499
                               [('modify', ('a', b'a\nB\nb\nC\nc\nE\n'))],
 
1500
                               revision_id=b'E-id')
 
1501
        builder.build_snapshot([b'B-id', b'C-id'],
 
1502
                               [('unversion', 'a')],
 
1503
                               revision_id=b'D-id')
 
1504
        merge_obj = self.make_merge_obj(builder, b'E-id')
1397
1505
 
1398
 
        self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1399
 
                                            for t in merge_obj._lca_trees])
1400
 
        self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
 
1506
        self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
 
1507
                                              for t in merge_obj._lca_trees])
 
1508
        self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1401
1509
 
1402
1510
        entries = list(merge_obj._entries_lca())
1403
 
        root_id = 'a-root-id'
1404
 
        self.assertEqual([('a-id', True,
 
1511
        root_id = b'a-root-id'
 
1512
        self.assertEqual([(b'a-id', True,
 
1513
                           ((u'a', [u'a', u'a']), u'a', None),
1405
1514
                           ((root_id, [root_id, root_id]), root_id, None),
1406
1515
                           ((u'a', [u'a', u'a']), u'a', None),
1407
 
                           ((False, [False, False]), False, None)),
1408
 
                         ], entries)
 
1516
                           ((False, [False, False]), False, None),
 
1517
                           False),
 
1518
                          ], entries)
1409
1519
 
1410
1520
    def test_file_not_in_one_lca(self):
1411
1521
        #   A   # just root
1414
1524
        #   |X|
1415
1525
        #   D E # D and E both have the file, unchanged from C
1416
1526
        builder = self.get_builder()
1417
 
        builder.build_snapshot('A-id', None,
1418
 
            [('add', (u'', 'a-root-id', 'directory', None))])
1419
 
        builder.build_snapshot('B-id', ['A-id'], [])
1420
 
        builder.build_snapshot('C-id', ['A-id'],
1421
 
            [('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1422
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], []) # Inherited from C
1423
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], # Merged from C
1424
 
            [('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1425
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1527
        builder.build_snapshot(None,
 
1528
                               [('add', (u'', b'a-root-id', 'directory', None))],
 
1529
                               revision_id=b'A-id')
 
1530
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1531
        builder.build_snapshot([b'A-id'],
 
1532
                               [('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1533
                               revision_id=b'C-id')
 
1534
        builder.build_snapshot([b'C-id', b'B-id'],
 
1535
                               [], revision_id=b'E-id')  # Inherited from C
 
1536
        builder.build_snapshot([b'B-id', b'C-id'],  # Merged from C
 
1537
                               [('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1538
                               revision_id=b'D-id')
 
1539
        merge_obj = self.make_merge_obj(builder, b'E-id')
1426
1540
 
1427
 
        self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1428
 
                                            for t in merge_obj._lca_trees])
1429
 
        self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
 
1541
        self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
 
1542
                                              for t in merge_obj._lca_trees])
 
1543
        self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1430
1544
 
1431
1545
        entries = list(merge_obj._entries_lca())
1432
1546
        self.assertEqual([], entries)
1433
1547
 
1434
1548
    def test_not_in_other(self):
1435
1549
        builder = self.get_builder()
1436
 
        builder.build_snapshot('A-id', None,
1437
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1438
 
             ('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1439
 
        builder.build_snapshot('B-id', ['A-id'], [])
1440
 
        builder.build_snapshot('C-id', ['A-id'], [])
1441
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1442
 
            [('unversion', 'a-id')])
1443
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1444
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1550
        builder.build_snapshot(None,
 
1551
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1552
                                ('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1553
                               revision_id=b'A-id')
 
1554
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1555
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1556
        builder.build_snapshot(
 
1557
            [b'C-id', b'B-id'],
 
1558
            [('unversion', 'a')], revision_id=b'E-id')
 
1559
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1560
        merge_obj = self.make_merge_obj(builder, b'E-id')
1445
1561
 
1446
1562
        entries = list(merge_obj._entries_lca())
1447
 
        root_id = 'a-root-id'
1448
 
        self.assertEqual([('a-id', True,
 
1563
        root_id = b'a-root-id'
 
1564
        self.assertEqual([(b'a-id', True,
 
1565
                           ((u'a', [u'a', u'a']), None, u'a'),
1449
1566
                           ((root_id, [root_id, root_id]), None, root_id),
1450
1567
                           ((u'a', [u'a', u'a']), None, u'a'),
1451
 
                           ((False, [False, False]), None, False)),
1452
 
                         ], entries)
 
1568
                           ((False, [False, False]), None, False),
 
1569
                           False),
 
1570
                          ], entries)
1453
1571
 
1454
1572
    def test_not_in_other_or_lca(self):
1455
1573
        #       A    base, introduces 'foo'
1464
1582
        #   C => E, no changes
1465
1583
        # D would then win 'cleanly' and no record would be given
1466
1584
        builder = self.get_builder()
1467
 
        builder.build_snapshot('A-id', None,
1468
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1469
 
             ('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1470
 
        builder.build_snapshot('B-id', ['A-id'], [])
1471
 
        builder.build_snapshot('C-id', ['A-id'],
1472
 
            [('unversion', 'foo-id')])
1473
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1474
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1475
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1585
        builder.build_snapshot(None,
 
1586
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1587
                                ('add', (u'foo', b'foo-id', 'file', b'content\n'))],
 
1588
                               revision_id=b'A-id')
 
1589
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1590
        builder.build_snapshot([b'A-id'],
 
1591
                               [('unversion', 'foo')], revision_id=b'C-id')
 
1592
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1593
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1594
        merge_obj = self.make_merge_obj(builder, b'E-id')
1476
1595
 
1477
1596
        entries = list(merge_obj._entries_lca())
1478
1597
        self.assertEqual([], entries)
1493
1612
        # In this case, we have a conflict of how the changes were resolved. E
1494
1613
        # picked C and D picked B, so we should issue a conflict
1495
1614
        builder = self.get_builder()
1496
 
        builder.build_snapshot('A-id', None,
1497
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1498
 
             ('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1499
 
        builder.build_snapshot('B-id', ['A-id'], [
1500
 
            ('modify', ('foo-id', 'new-content\n'))])
1501
 
        builder.build_snapshot('C-id', ['A-id'],
1502
 
            [('unversion', 'foo-id')])
1503
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1504
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1505
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1615
        builder.build_snapshot(None,
 
1616
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1617
                                ('add', (u'foo', b'foo-id', 'file', b'content\n'))],
 
1618
                               revision_id=b'A-id')
 
1619
        builder.build_snapshot([b'A-id'], [
 
1620
            ('modify', ('foo', b'new-content\n'))],
 
1621
            revision_id=b'B-id')
 
1622
        builder.build_snapshot([b'A-id'],
 
1623
                               [('unversion', 'foo')],
 
1624
                               revision_id=b'C-id')
 
1625
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1626
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1627
        merge_obj = self.make_merge_obj(builder, b'E-id')
1506
1628
 
1507
1629
        entries = list(merge_obj._entries_lca())
1508
 
        root_id = 'a-root-id'
1509
 
        self.assertEqual([('foo-id', True,
 
1630
        root_id = b'a-root-id'
 
1631
        self.assertEqual([(b'foo-id', True,
 
1632
                           ((u'foo', [u'foo', None]), None, u'foo'),
1510
1633
                           ((root_id, [root_id, None]), None, root_id),
1511
1634
                           ((u'foo', [u'foo', None]), None, 'foo'),
1512
 
                           ((False, [False, None]), None, False)),
1513
 
                         ], entries)
 
1635
                           ((False, [False, None]), None, False),
 
1636
                           False),
 
1637
                          ], entries)
1514
1638
 
1515
1639
    def test_only_in_one_lca(self):
1516
1640
        #   A   add only root
1529
1653
        #   A => C, add file, thus C supersedes B
1530
1654
        #   w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1531
1655
        builder = self.get_builder()
1532
 
        builder.build_snapshot('A-id', None,
1533
 
            [('add', (u'', 'a-root-id', 'directory', None))])
1534
 
        builder.build_snapshot('B-id', ['A-id'], [])
1535
 
        builder.build_snapshot('C-id', ['A-id'],
1536
 
            [('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1537
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1538
 
            [('unversion', 'a-id')])
1539
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1540
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1656
        builder.build_snapshot(None,
 
1657
                               [('add', (u'', b'a-root-id', 'directory', None))],
 
1658
                               revision_id=b'A-id')
 
1659
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1660
        builder.build_snapshot([b'A-id'],
 
1661
                               [('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1662
                               revision_id=b'C-id')
 
1663
        builder.build_snapshot([b'C-id', b'B-id'],
 
1664
                               [('unversion', 'a')],
 
1665
                               revision_id=b'E-id')
 
1666
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1667
        merge_obj = self.make_merge_obj(builder, b'E-id')
1541
1668
 
1542
1669
        entries = list(merge_obj._entries_lca())
1543
1670
        self.assertEqual([], entries)
1544
1671
 
1545
1672
    def test_only_in_other(self):
1546
1673
        builder = self.get_builder()
1547
 
        builder.build_snapshot('A-id', None,
1548
 
            [('add', (u'', 'a-root-id', 'directory', None))])
1549
 
        builder.build_snapshot('B-id', ['A-id'], [])
1550
 
        builder.build_snapshot('C-id', ['A-id'], [])
1551
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1552
 
            [('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1553
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1554
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1674
        builder.build_snapshot(None,
 
1675
                               [('add', (u'', b'a-root-id', 'directory', None))],
 
1676
                               revision_id=b'A-id')
 
1677
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1678
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1679
        builder.build_snapshot([b'C-id', b'B-id'],
 
1680
                               [('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
1681
                               revision_id=b'E-id')
 
1682
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1683
        merge_obj = self.make_merge_obj(builder, b'E-id')
1555
1684
 
1556
1685
        entries = list(merge_obj._entries_lca())
1557
 
        root_id = 'a-root-id'
1558
 
        self.assertEqual([('a-id', True,
 
1686
        root_id = b'a-root-id'
 
1687
        self.assertEqual([(b'a-id', True,
 
1688
                           ((None, [None, None]), u'a', None),
1559
1689
                           ((None, [None, None]), root_id, None),
1560
1690
                           ((None, [None, None]), u'a', None),
1561
 
                           ((None, [None, None]), False, None)),
1562
 
                         ], entries)
 
1691
                           ((None, [None, None]), False, None),
 
1692
                           False),
 
1693
                          ], entries)
1563
1694
 
1564
1695
    def test_one_lca_supersedes(self):
1565
1696
        # One LCA supersedes the other LCAs last modified value, but the
1576
1707
        #   though its LCAs disagree. This is because the modification in E
1577
1708
        #   completely supersedes the value in D.
1578
1709
        builder = self.get_builder()
1579
 
        builder.build_snapshot('A-id', None,
1580
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1581
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1582
 
        builder.build_snapshot('C-id', ['A-id'], [])
1583
 
        builder.build_snapshot('B-id', ['A-id'],
1584
 
            [('modify', ('foo-id', 'B content\n'))])
1585
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1586
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1587
 
            [('modify', ('foo-id', 'E content\n'))])
1588
 
        builder.build_snapshot('G-id', ['E-id', 'D-id'], [])
1589
 
        builder.build_snapshot('F-id', ['D-id', 'E-id'],
1590
 
            [('modify', ('foo-id', 'F content\n'))])
1591
 
        merge_obj = self.make_merge_obj(builder, 'G-id')
 
1710
        builder.build_snapshot(None,
 
1711
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1712
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
1713
                               revision_id=b'A-id')
 
1714
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1715
        builder.build_snapshot([b'A-id'],
 
1716
                               [('modify', ('foo', b'B content\n'))],
 
1717
                               revision_id=b'B-id')
 
1718
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1719
        builder.build_snapshot([b'C-id', b'B-id'],
 
1720
                               [('modify', ('foo', b'E content\n'))],
 
1721
                               revision_id=b'E-id')
 
1722
        builder.build_snapshot([b'E-id', b'D-id'], [], revision_id=b'G-id')
 
1723
        builder.build_snapshot([b'D-id', b'E-id'],
 
1724
                               [('modify', ('foo', b'F content\n'))],
 
1725
                               revision_id=b'F-id')
 
1726
        merge_obj = self.make_merge_obj(builder, b'G-id')
1592
1727
 
1593
1728
        self.assertEqual([], list(merge_obj._entries_lca()))
1594
1729
 
1622
1757
        # aren't supporting it yet.
1623
1758
        #
1624
1759
        builder = self.get_builder()
1625
 
        builder.build_snapshot('A-id', None,
1626
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1627
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1628
 
        builder.build_snapshot('C-id', ['A-id'], [])
1629
 
        builder.build_snapshot('B-id', ['A-id'],
1630
 
            [('rename', ('foo', 'bar'))])
1631
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1632
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1633
 
            [('rename', ('foo', 'bing'))]) # override to bing
1634
 
        builder.build_snapshot('G-id', ['E-id', 'D-id'],
1635
 
            [('rename', ('bing', 'barry'))]) # override to barry
1636
 
        builder.build_snapshot('F-id', ['D-id', 'E-id'],
1637
 
            [('rename', ('bar', 'bing'))]) # Merge in E's change
1638
 
        merge_obj = self.make_merge_obj(builder, 'G-id')
 
1760
        builder.build_snapshot(None,
 
1761
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1762
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
1763
                               revision_id=b'A-id')
 
1764
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1765
        builder.build_snapshot([b'A-id'],
 
1766
                               [('rename', ('foo', 'bar'))],
 
1767
                               revision_id=b'B-id')
 
1768
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1769
        builder.build_snapshot([b'C-id', b'B-id'],
 
1770
                               [('rename', ('foo', 'bing'))],
 
1771
                               revision_id=b'E-id')  # override to bing
 
1772
        builder.build_snapshot([b'E-id', b'D-id'],
 
1773
                               [('rename', ('bing', 'barry'))],
 
1774
                               revision_id=b'G-id')  # override to barry
 
1775
        builder.build_snapshot([b'D-id', b'E-id'],
 
1776
                               [('rename', ('bar', 'bing'))],
 
1777
                               revision_id=b'F-id')  # Merge in E's change
 
1778
        merge_obj = self.make_merge_obj(builder, b'G-id')
1639
1779
 
1640
1780
        self.expectFailure("We don't do an actual heads() check on lca values,"
1641
 
            " or use the per-attribute graph",
1642
 
            self.assertEqual, [], list(merge_obj._entries_lca()))
 
1781
                           " or use the per-attribute graph",
 
1782
                           self.assertEqual, [], list(merge_obj._entries_lca()))
1643
1783
 
1644
1784
    def test_one_lca_accidentally_pruned(self):
1645
1785
        # Another incorrect resolution from the same basic flaw:
1655
1795
        # be pruned from the LCAs, even though it was newly introduced by E
1656
1796
        # (superseding B).
1657
1797
        builder = self.get_builder()
1658
 
        builder.build_snapshot('A-id', None,
1659
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1660
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1661
 
        builder.build_snapshot('C-id', ['A-id'], [])
1662
 
        builder.build_snapshot('B-id', ['A-id'],
1663
 
            [('rename', ('foo', 'bar'))])
1664
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1665
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1666
 
        builder.build_snapshot('G-id', ['E-id', 'D-id'],
1667
 
            [('rename', ('foo', 'bar'))])
1668
 
        builder.build_snapshot('F-id', ['D-id', 'E-id'],
1669
 
            [('rename', ('bar', 'bing'))]) # should end up conflicting
1670
 
        merge_obj = self.make_merge_obj(builder, 'G-id')
 
1798
        builder.build_snapshot(None,
 
1799
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1800
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
1801
                               revision_id=b'A-id')
 
1802
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1803
        builder.build_snapshot([b'A-id'],
 
1804
                               [('rename', ('foo', 'bar'))],
 
1805
                               revision_id=b'B-id')
 
1806
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1807
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1808
        builder.build_snapshot([b'E-id', b'D-id'],
 
1809
                               [('rename', ('foo', 'bar'))],
 
1810
                               revision_id=b'G-id')
 
1811
        builder.build_snapshot([b'D-id', b'E-id'],
 
1812
                               [('rename', ('bar', 'bing'))],
 
1813
                               revision_id=b'F-id')  # should end up conflicting
 
1814
        merge_obj = self.make_merge_obj(builder, b'G-id')
1671
1815
 
1672
1816
        entries = list(merge_obj._entries_lca())
1673
 
        root_id = 'a-root-id'
 
1817
        root_id = b'a-root-id'
1674
1818
        self.expectFailure("We prune values from BASE even when relevant.",
1675
 
            self.assertEqual,
1676
 
                [('foo-id', False,
1677
 
                  ((root_id, [root_id, root_id]), root_id, root_id),
1678
 
                  ((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1679
 
                  ((False, [False, False]), False, False)),
1680
 
                ], entries)
 
1819
                           self.assertEqual,
 
1820
                           [(b'foo-id', False,
 
1821
                             ((root_id, [root_id, root_id]), root_id, root_id),
 
1822
                               ((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
 
1823
                               ((False, [False, False]), False, False),
 
1824
                             False),
 
1825
                            ], entries)
1681
1826
 
1682
1827
    def test_both_sides_revert(self):
1683
1828
        # Both sides of a criss-cross revert the text to the lca
1688
1833
        #       D E  D reverts to B, E reverts to C
1689
1834
        # This should conflict
1690
1835
        builder = self.get_builder()
1691
 
        builder.build_snapshot('A-id', None,
1692
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1693
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1694
 
        builder.build_snapshot('B-id', ['A-id'],
1695
 
            [('modify', ('foo-id', 'B content\n'))])
1696
 
        builder.build_snapshot('C-id', ['A-id'],
1697
 
            [('modify', ('foo-id', 'C content\n'))])
1698
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1699
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1700
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1836
        builder.build_snapshot(None,
 
1837
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1838
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
1839
                               revision_id=b'A-id')
 
1840
        builder.build_snapshot([b'A-id'],
 
1841
                               [('modify', ('foo', b'B content\n'))],
 
1842
                               revision_id=b'B-id')
 
1843
        builder.build_snapshot([b'A-id'],
 
1844
                               [('modify', ('foo', b'C content\n'))],
 
1845
                               revision_id=b'C-id')
 
1846
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1847
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1848
        merge_obj = self.make_merge_obj(builder, b'E-id')
1701
1849
 
1702
1850
        entries = list(merge_obj._entries_lca())
1703
 
        root_id = 'a-root-id'
1704
 
        self.assertEqual([('foo-id', True,
 
1851
        root_id = b'a-root-id'
 
1852
        self.assertEqual([(b'foo-id', True,
 
1853
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1705
1854
                           ((root_id, [root_id, root_id]), root_id, root_id),
1706
1855
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1707
 
                           ((False, [False, False]), False, False)),
1708
 
                         ], entries)
 
1856
                           ((False, [False, False]), False, False),
 
1857
                           False),
 
1858
                          ], entries)
1709
1859
 
1710
1860
    def test_different_lca_resolve_one_side_updates_content(self):
1711
1861
        # Both sides converge, but then one side updates the text.
1719
1869
        # We need to emit an entry for 'foo', because D & E differed on the
1720
1870
        # merge resolution
1721
1871
        builder = self.get_builder()
1722
 
        builder.build_snapshot('A-id', None,
1723
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1724
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1725
 
        builder.build_snapshot('B-id', ['A-id'],
1726
 
            [('modify', ('foo-id', 'B content\n'))])
1727
 
        builder.build_snapshot('C-id', ['A-id'],
1728
 
            [('modify', ('foo-id', 'C content\n'))])
1729
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1730
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1731
 
        builder.build_snapshot('F-id', ['D-id'],
1732
 
            [('modify', ('foo-id', 'F content\n'))])
1733
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1872
        builder.build_snapshot(None,
 
1873
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1874
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
1875
                               revision_id=b'A-id')
 
1876
        builder.build_snapshot([b'A-id'],
 
1877
                               [('modify', ('foo', b'B content\n'))],
 
1878
                               revision_id=b'B-id')
 
1879
        builder.build_snapshot([b'A-id'],
 
1880
                               [('modify', ('foo', b'C content\n'))],
 
1881
                               revision_id=b'C-id', )
 
1882
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1883
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1884
        builder.build_snapshot([b'D-id'],
 
1885
                               [('modify', ('foo', b'F content\n'))],
 
1886
                               revision_id=b'F-id')
 
1887
        merge_obj = self.make_merge_obj(builder, b'E-id')
1734
1888
 
1735
1889
        entries = list(merge_obj._entries_lca())
1736
 
        root_id = 'a-root-id'
1737
 
        self.assertEqual([('foo-id', True,
 
1890
        root_id = b'a-root-id'
 
1891
        self.assertEqual([(b'foo-id', True,
 
1892
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1738
1893
                           ((root_id, [root_id, root_id]), root_id, root_id),
1739
1894
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1740
 
                           ((False, [False, False]), False, False)),
1741
 
                         ], entries)
 
1895
                           ((False, [False, False]), False, False),
 
1896
                           False),
 
1897
                          ], entries)
1742
1898
 
1743
1899
    def test_same_lca_resolution_one_side_updates_content(self):
1744
1900
        # Both sides converge, but then one side updates the text.
1755
1911
        # We need to conflict.
1756
1912
 
1757
1913
        builder = self.get_builder()
1758
 
        builder.build_snapshot('A-id', None,
1759
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1760
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1761
 
        builder.build_snapshot('B-id', ['A-id'],
1762
 
            [('modify', ('foo-id', 'B content\n'))])
1763
 
        builder.build_snapshot('C-id', ['A-id'],
1764
 
            [('modify', ('foo-id', 'C content\n'))])
1765
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1766
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1767
 
            [('modify', ('foo-id', 'C content\n'))]) # Same as E
1768
 
        builder.build_snapshot('F-id', ['D-id'],
1769
 
            [('modify', ('foo-id', 'F content\n'))])
1770
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1914
        builder.build_snapshot(None,
 
1915
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1916
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
1917
                               revision_id=b'A-id')
 
1918
        builder.build_snapshot([b'A-id'],
 
1919
                               [('modify', ('foo', b'B content\n'))],
 
1920
                               revision_id=b'B-id')
 
1921
        builder.build_snapshot([b'A-id'],
 
1922
                               [('modify', ('foo', b'C content\n'))],
 
1923
                               revision_id=b'C-id')
 
1924
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1925
        builder.build_snapshot([b'B-id', b'C-id'],
 
1926
                               [('modify', ('foo', b'C content\n'))],
 
1927
                               revision_id=b'D-id')  # Same as E
 
1928
        builder.build_snapshot([b'D-id'],
 
1929
                               [('modify', ('foo', b'F content\n'))],
 
1930
                               revision_id=b'F-id')
 
1931
        merge_obj = self.make_merge_obj(builder, b'E-id')
1771
1932
 
1772
1933
        entries = list(merge_obj._entries_lca())
1773
1934
        self.expectFailure("We don't detect that LCA resolution was the"
1774
1935
                           " same on both sides",
1775
 
            self.assertEqual, [], entries)
 
1936
                           self.assertEqual, [], entries)
1776
1937
 
1777
1938
    def test_only_path_changed(self):
1778
1939
        builder = self.get_builder()
1779
 
        builder.build_snapshot('A-id', None,
1780
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1781
 
             ('add', (u'a', 'a-id', 'file', 'content\n'))])
1782
 
        builder.build_snapshot('B-id', ['A-id'], [])
1783
 
        builder.build_snapshot('C-id', ['A-id'], [])
1784
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1785
 
            [('rename', (u'a', u'b'))])
1786
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1787
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1940
        builder.build_snapshot(None,
 
1941
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1942
                                ('add', (u'a', b'a-id', 'file', b'content\n'))],
 
1943
                               revision_id=b'A-id')
 
1944
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1945
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1946
        builder.build_snapshot([b'C-id', b'B-id'],
 
1947
                               [('rename', (u'a', u'b'))],
 
1948
                               revision_id=b'E-id')
 
1949
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1950
        merge_obj = self.make_merge_obj(builder, b'E-id')
1788
1951
        entries = list(merge_obj._entries_lca())
1789
 
        root_id = 'a-root-id'
 
1952
        root_id = b'a-root-id'
1790
1953
        # The content was not changed, only the path
1791
 
        self.assertEqual([('a-id', False,
 
1954
        self.assertEqual([(b'a-id', False,
 
1955
                           ((u'a', [u'a', u'a']), u'b', u'a'),
1792
1956
                           ((root_id, [root_id, root_id]), root_id, root_id),
1793
1957
                           ((u'a', [u'a', u'a']), u'b', u'a'),
1794
 
                           ((False, [False, False]), False, False)),
1795
 
                         ], entries)
 
1958
                           ((False, [False, False]), False, False),
 
1959
                           False),
 
1960
                          ], entries)
1796
1961
 
1797
1962
    def test_kind_changed(self):
1798
1963
        # Identical content, except 'D' changes a-id into a directory
1799
1964
        builder = self.get_builder()
1800
 
        builder.build_snapshot('A-id', None,
1801
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1802
 
             ('add', (u'a', 'a-id', 'file', 'content\n'))])
1803
 
        builder.build_snapshot('B-id', ['A-id'], [])
1804
 
        builder.build_snapshot('C-id', ['A-id'], [])
1805
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1806
 
            [('unversion', 'a-id'),
1807
 
             ('add', (u'a', 'a-id', 'directory', None))])
1808
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1809
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1965
        builder.build_snapshot(None,
 
1966
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1967
                                ('add', (u'a', b'a-id', 'file', b'content\n'))],
 
1968
                               revision_id=b'A-id')
 
1969
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1970
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1971
        builder.build_snapshot([b'C-id', b'B-id'],
 
1972
                               [('unversion', 'a'),
 
1973
                                ('flush', None),
 
1974
                                ('add', (u'a', b'a-id', 'directory', None))],
 
1975
                               revision_id=b'E-id')
 
1976
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
1977
        merge_obj = self.make_merge_obj(builder, b'E-id')
1810
1978
        entries = list(merge_obj._entries_lca())
1811
 
        root_id = 'a-root-id'
 
1979
        root_id = b'a-root-id'
1812
1980
        # Only the kind was changed (content)
1813
 
        self.assertEqual([('a-id', True,
 
1981
        self.assertEqual([(b'a-id', True,
 
1982
                           ((u'a', [u'a', u'a']), u'a', u'a'),
1814
1983
                           ((root_id, [root_id, root_id]), root_id, root_id),
1815
1984
                           ((u'a', [u'a', u'a']), u'a', u'a'),
1816
 
                           ((False, [False, False]), False, False)),
1817
 
                         ], entries)
 
1985
                           ((False, [False, False]), False, False),
 
1986
                           False),
 
1987
                          ], entries)
1818
1988
 
1819
1989
    def test_this_changed_kind(self):
1820
1990
        # Identical content, but THIS changes a file to a directory
1821
1991
        builder = self.get_builder()
1822
 
        builder.build_snapshot('A-id', None,
1823
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1824
 
             ('add', (u'a', 'a-id', 'file', 'content\n'))])
1825
 
        builder.build_snapshot('B-id', ['A-id'], [])
1826
 
        builder.build_snapshot('C-id', ['A-id'], [])
1827
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1828
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1829
 
            [('unversion', 'a-id'),
1830
 
             ('add', (u'a', 'a-id', 'directory', None))])
1831
 
        merge_obj = self.make_merge_obj(builder, 'E-id')
 
1992
        builder.build_snapshot(None,
 
1993
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
1994
                                ('add', (u'a', b'a-id', 'file', b'content\n'))],
 
1995
                               revision_id=b'A-id')
 
1996
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
1997
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
1998
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
1999
        builder.build_snapshot([b'B-id', b'C-id'],
 
2000
                               [('unversion', 'a'),
 
2001
                                ('flush', None),
 
2002
                                ('add', (u'a', b'a-id', 'directory', None))],
 
2003
                               revision_id=b'D-id')
 
2004
        merge_obj = self.make_merge_obj(builder, b'E-id')
1832
2005
        entries = list(merge_obj._entries_lca())
1833
2006
        # Only the kind was changed (content)
1834
2007
        self.assertEqual([], entries)
1836
2009
    def test_interesting_files(self):
1837
2010
        # Two files modified, but we should filter one of them
1838
2011
        builder = self.get_builder()
1839
 
        builder.build_snapshot('A-id', None,
1840
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1841
 
             ('add', (u'a', 'a-id', 'file', 'content\n')),
1842
 
             ('add', (u'b', 'b-id', 'file', 'content\n'))])
1843
 
        builder.build_snapshot('B-id', ['A-id'], [])
1844
 
        builder.build_snapshot('C-id', ['A-id'], [])
1845
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1846
 
            [('modify', ('a-id', 'new-content\n')),
1847
 
             ('modify', ('b-id', 'new-content\n'))])
1848
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1849
 
        merge_obj = self.make_merge_obj(builder, 'E-id',
 
2012
        builder.build_snapshot(None,
 
2013
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2014
                                ('add', (u'a', b'a-id', 'file', b'content\n')),
 
2015
                                   ('add', (u'b', b'b-id', 'file', b'content\n'))],
 
2016
                               revision_id=b'A-id')
 
2017
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
2018
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2019
        builder.build_snapshot([b'C-id', b'B-id'],
 
2020
                               [('modify', ('a', b'new-content\n')),
 
2021
                                ('modify', ('b', b'new-content\n'))],
 
2022
                               revision_id=b'E-id')
 
2023
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2024
        merge_obj = self.make_merge_obj(builder, b'E-id',
1850
2025
                                        interesting_files=['b'])
1851
2026
        entries = list(merge_obj._entries_lca())
1852
 
        root_id = 'a-root-id'
1853
 
        self.assertEqual([('b-id', True,
 
2027
        root_id = b'a-root-id'
 
2028
        self.assertEqual([(b'b-id', True,
 
2029
                           ((u'b', [u'b', u'b']), u'b', u'b'),
1854
2030
                           ((root_id, [root_id, root_id]), root_id, root_id),
1855
2031
                           ((u'b', [u'b', u'b']), u'b', u'b'),
1856
 
                           ((False, [False, False]), False, False)),
1857
 
                         ], entries)
 
2032
                           ((False, [False, False]), False, False),
 
2033
                           False),
 
2034
                          ], entries)
1858
2035
 
1859
2036
    def test_interesting_file_in_this(self):
1860
2037
        # This renamed the file, but it should still match the entry in other
1861
2038
        builder = self.get_builder()
1862
 
        builder.build_snapshot('A-id', None,
1863
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1864
 
             ('add', (u'a', 'a-id', 'file', 'content\n')),
1865
 
             ('add', (u'b', 'b-id', 'file', 'content\n'))])
1866
 
        builder.build_snapshot('B-id', ['A-id'], [])
1867
 
        builder.build_snapshot('C-id', ['A-id'], [])
1868
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1869
 
            [('modify', ('a-id', 'new-content\n')),
1870
 
             ('modify', ('b-id', 'new-content\n'))])
1871
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1872
 
            [('rename', ('b', 'c'))])
1873
 
        merge_obj = self.make_merge_obj(builder, 'E-id',
 
2039
        builder.build_snapshot(None,
 
2040
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2041
                                ('add', (u'a', b'a-id', 'file', b'content\n')),
 
2042
                                   ('add', (u'b', b'b-id', 'file', b'content\n'))],
 
2043
                               revision_id=b'A-id')
 
2044
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
2045
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2046
        builder.build_snapshot([b'C-id', b'B-id'],
 
2047
                               [('modify', ('a', b'new-content\n')),
 
2048
                                ('modify', ('b', b'new-content\n'))],
 
2049
                               revision_id=b'E-id')
 
2050
        builder.build_snapshot([b'B-id', b'C-id'],
 
2051
                               [('rename', ('b', 'c'))],
 
2052
                               revision_id=b'D-id')
 
2053
        merge_obj = self.make_merge_obj(builder, b'E-id',
1874
2054
                                        interesting_files=['c'])
1875
2055
        entries = list(merge_obj._entries_lca())
1876
 
        root_id = 'a-root-id'
1877
 
        self.assertEqual([('b-id', True,
 
2056
        root_id = b'a-root-id'
 
2057
        self.assertEqual([(b'b-id', True,
 
2058
                           ((u'b', [u'b', u'b']), u'b', u'c'),
1878
2059
                           ((root_id, [root_id, root_id]), root_id, root_id),
1879
2060
                           ((u'b', [u'b', u'b']), u'b', u'c'),
1880
 
                           ((False, [False, False]), False, False)),
1881
 
                         ], entries)
 
2061
                           ((False, [False, False]), False, False),
 
2062
                           False),
 
2063
                          ], entries)
1882
2064
 
1883
2065
    def test_interesting_file_in_base(self):
1884
2066
        # This renamed the file, but it should still match the entry in BASE
1885
2067
        builder = self.get_builder()
1886
 
        builder.build_snapshot('A-id', None,
1887
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1888
 
             ('add', (u'a', 'a-id', 'file', 'content\n')),
1889
 
             ('add', (u'c', 'c-id', 'file', 'content\n'))])
1890
 
        builder.build_snapshot('B-id', ['A-id'],
1891
 
            [('rename', ('c', 'b'))])
1892
 
        builder.build_snapshot('C-id', ['A-id'],
1893
 
            [('rename', ('c', 'b'))])
1894
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1895
 
            [('modify', ('a-id', 'new-content\n')),
1896
 
             ('modify', ('c-id', 'new-content\n'))])
1897
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1898
 
        merge_obj = self.make_merge_obj(builder, 'E-id',
 
2068
        builder.build_snapshot(None,
 
2069
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2070
                                ('add', (u'a', b'a-id', 'file', b'content\n')),
 
2071
                                   ('add', (u'c', b'c-id', 'file', b'content\n'))],
 
2072
                               revision_id=b'A-id')
 
2073
        builder.build_snapshot([b'A-id'],
 
2074
                               [('rename', ('c', 'b'))],
 
2075
                               revision_id=b'B-id')
 
2076
        builder.build_snapshot([b'A-id'],
 
2077
                               [('rename', ('c', 'b'))],
 
2078
                               revision_id=b'C-id')
 
2079
        builder.build_snapshot([b'C-id', b'B-id'],
 
2080
                               [('modify', ('a', b'new-content\n')),
 
2081
                                ('modify', ('b', b'new-content\n'))],
 
2082
                               revision_id=b'E-id')
 
2083
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2084
        merge_obj = self.make_merge_obj(builder, b'E-id',
1899
2085
                                        interesting_files=['c'])
1900
2086
        entries = list(merge_obj._entries_lca())
1901
 
        root_id = 'a-root-id'
1902
 
        self.assertEqual([('c-id', True,
 
2087
        root_id = b'a-root-id'
 
2088
        self.assertEqual([(b'c-id', True,
 
2089
                           ((u'c', [u'b', u'b']), u'b', u'b'),
1903
2090
                           ((root_id, [root_id, root_id]), root_id, root_id),
1904
2091
                           ((u'c', [u'b', u'b']), u'b', u'b'),
1905
 
                           ((False, [False, False]), False, False)),
1906
 
                         ], entries)
 
2092
                           ((False, [False, False]), False, False),
 
2093
                           False),
 
2094
                          ], entries)
1907
2095
 
1908
2096
    def test_interesting_file_in_lca(self):
1909
2097
        # This renamed the file, but it should still match the entry in LCA
1910
2098
        builder = self.get_builder()
1911
 
        builder.build_snapshot('A-id', None,
1912
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1913
 
             ('add', (u'a', 'a-id', 'file', 'content\n')),
1914
 
             ('add', (u'b', 'b-id', 'file', 'content\n'))])
1915
 
        builder.build_snapshot('B-id', ['A-id'],
1916
 
            [('rename', ('b', 'c'))])
1917
 
        builder.build_snapshot('C-id', ['A-id'], [])
1918
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1919
 
            [('modify', ('a-id', 'new-content\n')),
1920
 
             ('modify', ('b-id', 'new-content\n'))])
1921
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1922
 
            [('rename', ('c', 'b'))])
1923
 
        merge_obj = self.make_merge_obj(builder, 'E-id',
 
2099
        builder.build_snapshot(None,
 
2100
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2101
                                ('add', (u'a', b'a-id', 'file', b'content\n')),
 
2102
                                   ('add', (u'b', b'b-id', 'file', b'content\n'))],
 
2103
                               revision_id=b'A-id')
 
2104
        builder.build_snapshot([b'A-id'],
 
2105
                               [('rename', ('b', 'c'))], revision_id=b'B-id')
 
2106
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2107
        builder.build_snapshot([b'C-id', b'B-id'],
 
2108
                               [('modify', ('a', b'new-content\n')),
 
2109
                                ('modify', ('b', b'new-content\n'))],
 
2110
                               revision_id=b'E-id')
 
2111
        builder.build_snapshot([b'B-id', b'C-id'],
 
2112
                               [('rename', ('c', 'b'))], revision_id=b'D-id')
 
2113
        merge_obj = self.make_merge_obj(builder, b'E-id',
1924
2114
                                        interesting_files=['c'])
1925
2115
        entries = list(merge_obj._entries_lca())
1926
 
        root_id = 'a-root-id'
1927
 
        self.assertEqual([('b-id', True,
 
2116
        root_id = b'a-root-id'
 
2117
        self.assertEqual([(b'b-id', True,
 
2118
                           ((u'b', [u'c', u'b']), u'b', u'b'),
1928
2119
                           ((root_id, [root_id, root_id]), root_id, root_id),
1929
2120
                           ((u'b', [u'c', u'b']), u'b', u'b'),
1930
 
                           ((False, [False, False]), False, False)),
1931
 
                         ], entries)
 
2121
                           ((False, [False, False]), False, False),
 
2122
                           False),
 
2123
                          ], entries)
1932
2124
 
1933
 
    def test_interesting_ids(self):
 
2125
    def test_interesting_files(self):
1934
2126
        # Two files modified, but we should filter one of them
1935
2127
        builder = self.get_builder()
1936
 
        builder.build_snapshot('A-id', None,
1937
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1938
 
             ('add', (u'a', 'a-id', 'file', 'content\n')),
1939
 
             ('add', (u'b', 'b-id', 'file', 'content\n'))])
1940
 
        builder.build_snapshot('B-id', ['A-id'], [])
1941
 
        builder.build_snapshot('C-id', ['A-id'], [])
1942
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1943
 
            [('modify', ('a-id', 'new-content\n')),
1944
 
             ('modify', ('b-id', 'new-content\n'))])
1945
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1946
 
        merge_obj = self.make_merge_obj(builder, 'E-id',
1947
 
                                        interesting_ids=['b-id'])
 
2128
        builder.build_snapshot(None,
 
2129
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2130
                                ('add', (u'a', b'a-id', 'file', b'content\n')),
 
2131
                                   ('add', (u'b', b'b-id', 'file', b'content\n'))],
 
2132
                               revision_id=b'A-id')
 
2133
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
2134
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2135
        builder.build_snapshot([b'C-id', b'B-id'],
 
2136
                               [('modify', ('a', b'new-content\n')),
 
2137
                                ('modify', ('b', b'new-content\n'))], revision_id=b'E-id')
 
2138
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2139
        merge_obj = self.make_merge_obj(builder, b'E-id',
 
2140
                                        interesting_files=['b'])
1948
2141
        entries = list(merge_obj._entries_lca())
1949
 
        root_id = 'a-root-id'
1950
 
        self.assertEqual([('b-id', True,
 
2142
        root_id = b'a-root-id'
 
2143
        self.assertEqual([(b'b-id', True,
 
2144
                           ((u'b', [u'b', u'b']), u'b', u'b'),
1951
2145
                           ((root_id, [root_id, root_id]), root_id, root_id),
1952
2146
                           ((u'b', [u'b', u'b']), u'b', u'b'),
1953
 
                           ((False, [False, False]), False, False)),
1954
 
                         ], entries)
1955
 
 
 
2147
                           ((False, [False, False]), False, False),
 
2148
                           False),
 
2149
                          ], entries)
1956
2150
 
1957
2151
 
1958
2152
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
1966
2160
    def get_wt_from_builder(self, builder):
1967
2161
        """Get a real WorkingTree from the builder."""
1968
2162
        the_branch = builder.get_branch()
1969
 
        wt = the_branch.bzrdir.create_workingtree()
 
2163
        wt = the_branch.controldir.create_workingtree()
1970
2164
        # Note: This is a little bit ugly, but we are holding the branch
1971
2165
        #       write-locked as part of the build process, and we would like to
1972
2166
        #       maintain that. So we just force the WT to re-use the same
1978
2172
 
1979
2173
    def do_merge(self, builder, other_revision_id):
1980
2174
        wt = self.get_wt_from_builder(builder)
1981
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2175
        merger = _mod_merge.Merger.from_revision_ids(
1982
2176
            wt, other_revision_id)
1983
2177
        merger.merge_type = _mod_merge.Merge3Merger
1984
2178
        return wt, merger.do_merge()
1985
2179
 
1986
2180
    def test_simple_lca(self):
1987
2181
        builder = self.get_builder()
1988
 
        builder.build_snapshot('A-id', None,
1989
 
            [('add', (u'', 'a-root-id', 'directory', None)),
1990
 
             ('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1991
 
        builder.build_snapshot('C-id', ['A-id'], [])
1992
 
        builder.build_snapshot('B-id', ['A-id'], [])
1993
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1994
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1995
 
            [('modify', ('a-id', 'a\nb\nc\nd\ne\nf\n'))])
1996
 
        wt, conflicts = self.do_merge(builder, 'E-id')
 
2182
        builder.build_snapshot(None,
 
2183
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2184
                                ('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
 
2185
                               revision_id=b'A-id')
 
2186
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2187
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
2188
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
 
2189
        builder.build_snapshot([b'B-id', b'C-id'],
 
2190
                               [('modify', ('a', b'a\nb\nc\nd\ne\nf\n'))],
 
2191
                               revision_id=b'D-id')
 
2192
        wt, conflicts = self.do_merge(builder, b'E-id')
1997
2193
        self.assertEqual(0, conflicts)
1998
2194
        # The merge should have simply update the contents of 'a'
1999
 
        self.assertEqual('a\nb\nc\nd\ne\nf\n', wt.get_file_text('a-id'))
 
2195
        self.assertEqual(b'a\nb\nc\nd\ne\nf\n', wt.get_file_text('a'))
2000
2196
 
2001
2197
    def test_conflict_without_lca(self):
2002
2198
        # This test would cause a merge conflict, unless we use the lca trees
2011
2207
        #     |
2012
2208
        #     F     Path at 'baz' in F, which supersedes 'bar' and 'foo'
2013
2209
        builder = self.get_builder()
2014
 
        builder.build_snapshot('A-id', None,
2015
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2016
 
             ('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2017
 
        builder.build_snapshot('C-id', ['A-id'], [])
2018
 
        builder.build_snapshot('B-id', ['A-id'],
2019
 
            [('rename', ('foo', 'bar'))])
2020
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2021
 
            [('rename', ('foo', 'bar'))])
2022
 
        builder.build_snapshot('F-id', ['E-id'],
2023
 
            [('rename', ('bar', 'baz'))])
2024
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2025
 
        wt, conflicts = self.do_merge(builder, 'F-id')
 
2210
        builder.build_snapshot(None,
 
2211
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2212
                                ('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
 
2213
                               revision_id=b'A-id')
 
2214
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2215
        builder.build_snapshot([b'A-id'],
 
2216
                               [('rename', ('foo', 'bar'))], revision_id=b'B-id', )
 
2217
        builder.build_snapshot([b'C-id', b'B-id'],  # merge the rename
 
2218
                               [('rename', ('foo', 'bar'))], revision_id=b'E-id')
 
2219
        builder.build_snapshot([b'E-id'],
 
2220
                               [('rename', ('bar', 'baz'))], revision_id=b'F-id')
 
2221
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2222
        wt, conflicts = self.do_merge(builder, b'F-id')
2026
2223
        self.assertEqual(0, conflicts)
2027
2224
        # The merge should simply recognize that the final rename takes
2028
2225
        # precedence
2029
 
        self.assertEqual('baz', wt.id2path('foo-id'))
 
2226
        self.assertEqual('baz', wt.id2path(b'foo-id'))
2030
2227
 
2031
2228
    def test_other_deletes_lca_renames(self):
2032
2229
        # This test would cause a merge conflict, unless we use the lca trees
2041
2238
        #     |
2042
2239
        #     F     F deletes 'bar'
2043
2240
        builder = self.get_builder()
2044
 
        builder.build_snapshot('A-id', None,
2045
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2046
 
             ('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2047
 
        builder.build_snapshot('C-id', ['A-id'], [])
2048
 
        builder.build_snapshot('B-id', ['A-id'],
2049
 
            [('rename', ('foo', 'bar'))])
2050
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2051
 
            [('rename', ('foo', 'bar'))])
2052
 
        builder.build_snapshot('F-id', ['E-id'],
2053
 
            [('unversion', 'foo-id')])
2054
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2055
 
        wt, conflicts = self.do_merge(builder, 'F-id')
 
2241
        builder.build_snapshot(None,
 
2242
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2243
                                ('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
 
2244
                               revision_id=b'A-id')
 
2245
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2246
        builder.build_snapshot([b'A-id'],
 
2247
                               [('rename', ('foo', 'bar'))], revision_id=b'B-id')
 
2248
        builder.build_snapshot([b'C-id', b'B-id'],  # merge the rename
 
2249
                               [('rename', ('foo', 'bar'))], revision_id=b'E-id')
 
2250
        builder.build_snapshot([b'E-id'],
 
2251
                               [('unversion', 'bar')], revision_id=b'F-id')
 
2252
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2253
        wt, conflicts = self.do_merge(builder, b'F-id')
2056
2254
        self.assertEqual(0, conflicts)
2057
 
        self.assertRaises(errors.NoSuchId, wt.id2path, 'foo-id')
 
2255
        self.assertRaises(errors.NoSuchId, wt.id2path, b'foo-id')
2058
2256
 
2059
2257
    def test_executable_changes(self):
2060
2258
        #   A       Path at 'foo'
2067
2265
        #     |
2068
2266
        #     F     Executable bit changed
2069
2267
        builder = self.get_builder()
2070
 
        builder.build_snapshot('A-id', None,
2071
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2072
 
             ('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2073
 
        builder.build_snapshot('C-id', ['A-id'], [])
2074
 
        builder.build_snapshot('B-id', ['A-id'], [])
2075
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2076
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
 
2268
        builder.build_snapshot(None,
 
2269
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2270
                                ('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
 
2271
                               revision_id=b'A-id')
 
2272
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2273
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
2274
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2275
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
2077
2276
        # Have to use a real WT, because BranchBuilder doesn't support exec bit
2078
2277
        wt = self.get_wt_from_builder(builder)
2079
 
        tt = transform.TreeTransform(wt)
2080
 
        try:
2081
 
            tt.set_executability(True, tt.trans_id_tree_file_id('foo-id'))
 
2278
        with wt.transform() as tt:
 
2279
            tt.set_executability(True, tt.trans_id_tree_path('foo'))
2082
2280
            tt.apply()
2083
 
        except:
2084
 
            tt.finalize()
2085
 
            raise
2086
 
        self.assertTrue(wt.is_executable('foo-id'))
2087
 
        wt.commit('F-id', rev_id='F-id')
 
2281
        self.assertTrue(wt.is_executable('foo'))
 
2282
        wt.commit('F-id', rev_id=b'F-id')
2088
2283
        # Reset to D, so that we can merge F
2089
 
        wt.set_parent_ids(['D-id'])
2090
 
        wt.branch.set_last_revision_info(3, 'D-id')
 
2284
        wt.set_parent_ids([b'D-id'])
 
2285
        wt.branch.set_last_revision_info(3, b'D-id')
2091
2286
        wt.revert()
2092
 
        self.assertFalse(wt.is_executable('foo-id'))
2093
 
        conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
 
2287
        self.assertFalse(wt.is_executable('foo'))
 
2288
        conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2094
2289
        self.assertEqual(0, conflicts)
2095
 
        self.assertTrue(wt.is_executable('foo-id'))
 
2290
        self.assertTrue(wt.is_executable('foo'))
2096
2291
 
2097
2292
    def test_create_symlink(self):
2098
 
        self.requireFeature(tests.SymlinkFeature)
 
2293
        self.requireFeature(features.SymlinkFeature)
2099
2294
        #   A
2100
2295
        #  / \
2101
2296
        # B   C
2108
2303
        # Have to use a real WT, because BranchBuilder and MemoryTree don't
2109
2304
        # have symlink support
2110
2305
        builder = self.get_builder()
2111
 
        builder.build_snapshot('A-id', None,
2112
 
            [('add', (u'', 'a-root-id', 'directory', None))])
2113
 
        builder.build_snapshot('C-id', ['A-id'], [])
2114
 
        builder.build_snapshot('B-id', ['A-id'], [])
2115
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2116
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
 
2306
        builder.build_snapshot(None,
 
2307
                               [('add', (u'', b'a-root-id', 'directory', None))],
 
2308
                               revision_id=b'A-id')
 
2309
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2310
        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
 
2311
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2312
        builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
2117
2313
        # Have to use a real WT, because BranchBuilder doesn't support exec bit
2118
2314
        wt = self.get_wt_from_builder(builder)
2119
2315
        os.symlink('bar', 'path/foo')
2120
 
        wt.add(['foo'], ['foo-id'])
2121
 
        self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2122
 
        wt.commit('add symlink', rev_id='F-id')
 
2316
        wt.add(['foo'], [b'foo-id'])
 
2317
        self.assertEqual('bar', wt.get_symlink_target('foo'))
 
2318
        wt.commit('add symlink', rev_id=b'F-id')
2123
2319
        # Reset to D, so that we can merge F
2124
 
        wt.set_parent_ids(['D-id'])
2125
 
        wt.branch.set_last_revision_info(3, 'D-id')
 
2320
        wt.set_parent_ids([b'D-id'])
 
2321
        wt.branch.set_last_revision_info(3, b'D-id')
2126
2322
        wt.revert()
2127
 
        self.assertIs(None, wt.path2id('foo'))
2128
 
        conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
 
2323
        self.assertFalse(wt.is_versioned('foo'))
 
2324
        conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2129
2325
        self.assertEqual(0, conflicts)
2130
 
        self.assertEqual('foo-id', wt.path2id('foo'))
2131
 
        self.assertEqual('bar', wt.get_symlink_target('foo-id'))
 
2326
        self.assertEqual(b'foo-id', wt.path2id('foo'))
 
2327
        self.assertEqual('bar', wt.get_symlink_target('foo'))
2132
2328
 
2133
2329
    def test_both_sides_revert(self):
2134
2330
        # Both sides of a criss-cross revert the text to the lca
2141
2337
        # This must be done with a real WorkingTree, because normally their
2142
2338
        # inventory contains "None" rather than a real sha1
2143
2339
        builder = self.get_builder()
2144
 
        builder.build_snapshot('A-id', None,
2145
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2146
 
             ('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
2147
 
        builder.build_snapshot('B-id', ['A-id'],
2148
 
            [('modify', ('foo-id', 'B content\n'))])
2149
 
        builder.build_snapshot('C-id', ['A-id'],
2150
 
            [('modify', ('foo-id', 'C content\n'))])
2151
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2152
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2153
 
        wt, conflicts = self.do_merge(builder, 'E-id')
 
2340
        builder.build_snapshot(None,
 
2341
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2342
                                ('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
 
2343
                               revision_id=b'A-id')
 
2344
        builder.build_snapshot([b'A-id'],
 
2345
                               [('modify', ('foo', b'B content\n'))],
 
2346
                               revision_id=b'B-id')
 
2347
        builder.build_snapshot([b'A-id'],
 
2348
                               [('modify', ('foo', b'C content\n'))],
 
2349
                               revision_id=b'C-id')
 
2350
        builder.build_snapshot([b'C-id', b'B-id'], [],
 
2351
                               revision_id=b'E-id')
 
2352
        builder.build_snapshot([b'B-id', b'C-id'], [],
 
2353
                               revision_id=b'D-id')
 
2354
        wt, conflicts = self.do_merge(builder, b'E-id')
2154
2355
        self.assertEqual(1, conflicts)
2155
 
        self.assertEqualDiff('<<<<<<< TREE\n'
2156
 
                             'B content\n'
2157
 
                             '=======\n'
2158
 
                             'C content\n'
2159
 
                             '>>>>>>> MERGE-SOURCE\n',
2160
 
                             wt.get_file_text('foo-id'))
 
2356
        self.assertEqualDiff(b'<<<<<<< TREE\n'
 
2357
                             b'B content\n'
 
2358
                             b'=======\n'
 
2359
                             b'C content\n'
 
2360
                             b'>>>>>>> MERGE-SOURCE\n',
 
2361
                             wt.get_file_text('foo'))
2161
2362
 
2162
2363
    def test_modified_symlink(self):
2163
 
        self.requireFeature(tests.SymlinkFeature)
 
2364
        self.requireFeature(features.SymlinkFeature)
2164
2365
        #   A       Create symlink foo => bar
2165
2366
        #  / \
2166
2367
        # B   C     B relinks foo => baz
2180
2381
        wt.lock_write()
2181
2382
        self.addCleanup(wt.unlock)
2182
2383
        os.symlink('bar', 'path/foo')
2183
 
        wt.add(['foo'], ['foo-id'])
2184
 
        wt.commit('add symlink', rev_id='A-id')
 
2384
        wt.add(['foo'], [b'foo-id'])
 
2385
        wt.commit('add symlink', rev_id=b'A-id')
2185
2386
        os.remove('path/foo')
2186
2387
        os.symlink('baz', 'path/foo')
2187
 
        wt.commit('foo => baz', rev_id='B-id')
2188
 
        wt.set_last_revision('A-id')
2189
 
        wt.branch.set_last_revision_info(1, 'A-id')
 
2388
        wt.commit('foo => baz', rev_id=b'B-id')
 
2389
        wt.set_last_revision(b'A-id')
 
2390
        wt.branch.set_last_revision_info(1, b'A-id')
2190
2391
        wt.revert()
2191
 
        wt.commit('C', rev_id='C-id')
2192
 
        wt.merge_from_branch(wt.branch, 'B-id')
2193
 
        self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2194
 
        wt.commit('E merges C & B', rev_id='E-id')
 
2392
        wt.commit('C', rev_id=b'C-id')
 
2393
        wt.merge_from_branch(wt.branch, b'B-id')
 
2394
        self.assertEqual('baz', wt.get_symlink_target('foo'))
 
2395
        wt.commit('E merges C & B', rev_id=b'E-id')
2195
2396
        os.remove('path/foo')
2196
2397
        os.symlink('bing', 'path/foo')
2197
 
        wt.commit('F foo => bing', rev_id='F-id')
2198
 
        wt.set_last_revision('B-id')
2199
 
        wt.branch.set_last_revision_info(2, 'B-id')
 
2398
        wt.commit('F foo => bing', rev_id=b'F-id')
 
2399
        wt.set_last_revision(b'B-id')
 
2400
        wt.branch.set_last_revision_info(2, b'B-id')
2200
2401
        wt.revert()
2201
 
        wt.merge_from_branch(wt.branch, 'C-id')
2202
 
        wt.commit('D merges B & C', rev_id='D-id')
2203
 
        conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
 
2402
        wt.merge_from_branch(wt.branch, b'C-id')
 
2403
        wt.commit('D merges B & C', rev_id=b'D-id')
 
2404
        conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2204
2405
        self.assertEqual(0, conflicts)
2205
 
        self.assertEqual('bing', wt.get_symlink_target('foo-id'))
 
2406
        self.assertEqual('bing', wt.get_symlink_target('foo'))
2206
2407
 
2207
2408
    def test_renamed_symlink(self):
2208
 
        self.requireFeature(tests.SymlinkFeature)
 
2409
        self.requireFeature(features.SymlinkFeature)
2209
2410
        #   A       Create symlink foo => bar
2210
2411
        #  / \
2211
2412
        # B   C     B renames foo => barry
2223
2424
        wt.lock_write()
2224
2425
        self.addCleanup(wt.unlock)
2225
2426
        os.symlink('bar', 'path/foo')
2226
 
        wt.add(['foo'], ['foo-id'])
2227
 
        wt.commit('A add symlink', rev_id='A-id')
 
2427
        wt.add(['foo'], [b'foo-id'])
 
2428
        wt.commit('A add symlink', rev_id=b'A-id')
2228
2429
        wt.rename_one('foo', 'barry')
2229
 
        wt.commit('B foo => barry', rev_id='B-id')
2230
 
        wt.set_last_revision('A-id')
2231
 
        wt.branch.set_last_revision_info(1, 'A-id')
 
2430
        wt.commit('B foo => barry', rev_id=b'B-id')
 
2431
        wt.set_last_revision(b'A-id')
 
2432
        wt.branch.set_last_revision_info(1, b'A-id')
2232
2433
        wt.revert()
2233
 
        wt.commit('C', rev_id='C-id')
2234
 
        wt.merge_from_branch(wt.branch, 'B-id')
2235
 
        self.assertEqual('barry', wt.id2path('foo-id'))
2236
 
        self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2237
 
        wt.commit('E merges C & B', rev_id='E-id')
 
2434
        wt.commit('C', rev_id=b'C-id')
 
2435
        wt.merge_from_branch(wt.branch, b'B-id')
 
2436
        self.assertEqual('barry', wt.id2path(b'foo-id'))
 
2437
        self.assertEqual('bar', wt.get_symlink_target('barry'))
 
2438
        wt.commit('E merges C & B', rev_id=b'E-id')
2238
2439
        wt.rename_one('barry', 'blah')
2239
 
        wt.commit('F barry => blah', rev_id='F-id')
2240
 
        wt.set_last_revision('B-id')
2241
 
        wt.branch.set_last_revision_info(2, 'B-id')
 
2440
        wt.commit('F barry => blah', rev_id=b'F-id')
 
2441
        wt.set_last_revision(b'B-id')
 
2442
        wt.branch.set_last_revision_info(2, b'B-id')
2242
2443
        wt.revert()
2243
 
        wt.merge_from_branch(wt.branch, 'C-id')
2244
 
        wt.commit('D merges B & C', rev_id='D-id')
2245
 
        self.assertEqual('barry', wt.id2path('foo-id'))
 
2444
        wt.merge_from_branch(wt.branch, b'C-id')
 
2445
        wt.commit('D merges B & C', rev_id=b'D-id')
 
2446
        self.assertEqual('barry', wt.id2path(b'foo-id'))
2246
2447
        # Check the output of the Merger object directly
2247
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2248
 
            wt, 'F-id')
 
2448
        merger = _mod_merge.Merger.from_revision_ids(wt, b'F-id')
2249
2449
        merger.merge_type = _mod_merge.Merge3Merger
2250
2450
        merge_obj = merger.make_merger()
2251
2451
        root_id = wt.path2id('')
2252
2452
        entries = list(merge_obj._entries_lca())
2253
2453
        # No content change, just a path change
2254
 
        self.assertEqual([('foo-id', False,
 
2454
        self.assertEqual([(b'foo-id', False,
 
2455
                           ((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2255
2456
                           ((root_id, [root_id, root_id]), root_id, root_id),
2256
2457
                           ((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2257
 
                           ((False, [False, False]), False, False)),
2258
 
                         ], entries)
2259
 
        conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
 
2458
                           ((False, [False, False]), False, False),
 
2459
                           False),
 
2460
                          ], entries)
 
2461
        conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2260
2462
        self.assertEqual(0, conflicts)
2261
 
        self.assertEqual('blah', wt.id2path('foo-id'))
 
2463
        self.assertEqual('blah', wt.id2path(b'foo-id'))
2262
2464
 
2263
2465
    def test_symlink_no_content_change(self):
2264
 
        self.requireFeature(tests.SymlinkFeature)
 
2466
        self.requireFeature(features.SymlinkFeature)
2265
2467
        #   A       Create symlink foo => bar
2266
2468
        #  / \
2267
2469
        # B   C     B relinks foo => baz
2278
2480
        wt.lock_write()
2279
2481
        self.addCleanup(wt.unlock)
2280
2482
        os.symlink('bar', 'path/foo')
2281
 
        wt.add(['foo'], ['foo-id'])
2282
 
        wt.commit('add symlink', rev_id='A-id')
 
2483
        wt.add(['foo'], [b'foo-id'])
 
2484
        wt.commit('add symlink', rev_id=b'A-id')
2283
2485
        os.remove('path/foo')
2284
2486
        os.symlink('baz', 'path/foo')
2285
 
        wt.commit('foo => baz', rev_id='B-id')
2286
 
        wt.set_last_revision('A-id')
2287
 
        wt.branch.set_last_revision_info(1, 'A-id')
2288
 
        wt.revert()
2289
 
        wt.commit('C', rev_id='C-id')
2290
 
        wt.merge_from_branch(wt.branch, 'B-id')
2291
 
        self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2292
 
        wt.commit('E merges C & B', rev_id='E-id')
2293
 
        wt.set_last_revision('B-id')
2294
 
        wt.branch.set_last_revision_info(2, 'B-id')
2295
 
        wt.revert()
2296
 
        wt.merge_from_branch(wt.branch, 'C-id')
2297
 
        wt.commit('D merges B & C', rev_id='D-id')
 
2487
        wt.commit('foo => baz', rev_id=b'B-id')
 
2488
        wt.set_last_revision(b'A-id')
 
2489
        wt.branch.set_last_revision_info(1, b'A-id')
 
2490
        wt.revert()
 
2491
        wt.commit('C', rev_id=b'C-id')
 
2492
        wt.merge_from_branch(wt.branch, b'B-id')
 
2493
        self.assertEqual('baz', wt.get_symlink_target('foo'))
 
2494
        wt.commit('E merges C & B', rev_id=b'E-id')
 
2495
        wt.set_last_revision(b'B-id')
 
2496
        wt.branch.set_last_revision_info(2, b'B-id')
 
2497
        wt.revert()
 
2498
        wt.merge_from_branch(wt.branch, b'C-id')
 
2499
        wt.commit('D merges B & C', rev_id=b'D-id')
2298
2500
        os.remove('path/foo')
2299
2501
        os.symlink('bing', 'path/foo')
2300
 
        wt.commit('F foo => bing', rev_id='F-id')
 
2502
        wt.commit('F foo => bing', rev_id=b'F-id')
2301
2503
 
2302
2504
        # Check the output of the Merger object directly
2303
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2304
 
            wt, 'E-id')
 
2505
        merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2305
2506
        merger.merge_type = _mod_merge.Merge3Merger
2306
2507
        merge_obj = merger.make_merger()
2307
2508
        # Nothing interesting happened in OTHER relative to BASE
2308
2509
        self.assertEqual([], list(merge_obj._entries_lca()))
2309
2510
        # Now do a real merge, just to test the rest of the stack
2310
 
        conflicts = wt.merge_from_branch(wt.branch, to_revision='E-id')
 
2511
        conflicts = wt.merge_from_branch(wt.branch, to_revision=b'E-id')
2311
2512
        self.assertEqual(0, conflicts)
2312
 
        self.assertEqual('bing', wt.get_symlink_target('foo-id'))
 
2513
        self.assertEqual('bing', wt.get_symlink_target('foo'))
2313
2514
 
2314
2515
    def test_symlink_this_changed_kind(self):
2315
 
        self.requireFeature(tests.SymlinkFeature)
 
2516
        self.requireFeature(features.SymlinkFeature)
2316
2517
        #   A       Nothing
2317
2518
        #  / \
2318
2519
        # B   C     B creates symlink foo => bar
2326
2527
        wt = self.make_branch_and_tree('path')
2327
2528
        wt.lock_write()
2328
2529
        self.addCleanup(wt.unlock)
2329
 
        wt.commit('base', rev_id='A-id')
 
2530
        wt.commit('base', rev_id=b'A-id')
2330
2531
        os.symlink('bar', 'path/foo')
2331
 
        wt.add(['foo'], ['foo-id'])
2332
 
        wt.commit('add symlink foo => bar', rev_id='B-id')
2333
 
        wt.set_last_revision('A-id')
2334
 
        wt.branch.set_last_revision_info(1, 'A-id')
 
2532
        wt.add(['foo'], [b'foo-id'])
 
2533
        wt.commit('add symlink foo => bar', rev_id=b'B-id')
 
2534
        wt.set_last_revision(b'A-id')
 
2535
        wt.branch.set_last_revision_info(1, b'A-id')
2335
2536
        wt.revert()
2336
 
        wt.commit('C', rev_id='C-id')
2337
 
        wt.merge_from_branch(wt.branch, 'B-id')
2338
 
        self.assertEqual('bar', wt.get_symlink_target('foo-id'))
 
2537
        wt.commit('C', rev_id=b'C-id')
 
2538
        wt.merge_from_branch(wt.branch, b'B-id')
 
2539
        self.assertEqual('bar', wt.get_symlink_target('foo'))
2339
2540
        os.remove('path/foo')
2340
2541
        # We have to change the link in E, or it won't try to do a comparison
2341
2542
        os.symlink('bing', 'path/foo')
2342
 
        wt.commit('E merges C & B, overrides to bing', rev_id='E-id')
2343
 
        wt.set_last_revision('B-id')
2344
 
        wt.branch.set_last_revision_info(2, 'B-id')
 
2543
        wt.commit('E merges C & B, overrides to bing', rev_id=b'E-id')
 
2544
        wt.set_last_revision(b'B-id')
 
2545
        wt.branch.set_last_revision_info(2, b'B-id')
2345
2546
        wt.revert()
2346
 
        wt.merge_from_branch(wt.branch, 'C-id')
 
2547
        wt.merge_from_branch(wt.branch, b'C-id')
2347
2548
        os.remove('path/foo')
2348
 
        self.build_tree_contents([('path/foo', 'file content\n')])
 
2549
        self.build_tree_contents([('path/foo', b'file content\n')])
2349
2550
        # XXX: workaround, WT doesn't detect kind changes unless you do
2350
2551
        # iter_changes()
2351
2552
        list(wt.iter_changes(wt.basis_tree()))
2352
 
        wt.commit('D merges B & C, makes it a file', rev_id='D-id')
 
2553
        wt.commit('D merges B & C, makes it a file', rev_id=b'D-id')
2353
2554
 
2354
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2355
 
            wt, 'E-id')
 
2555
        merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2356
2556
        merger.merge_type = _mod_merge.Merge3Merger
2357
2557
        merge_obj = merger.make_merger()
2358
2558
        entries = list(merge_obj._entries_lca())
2359
2559
        root_id = wt.path2id('')
2360
 
        self.assertEqual([('foo-id', True,
 
2560
        self.assertEqual([(b'foo-id', True,
 
2561
                           ((None, [u'foo', None]), u'foo', u'foo'),
2361
2562
                           ((None, [root_id, None]), root_id, root_id),
2362
2563
                           ((None, [u'foo', None]), u'foo', u'foo'),
2363
 
                           ((None, [False, None]), False, False)),
2364
 
                         ], entries)
 
2564
                           ((None, [False, None]), False, False),
 
2565
                           False),
 
2566
                          ], entries)
2365
2567
 
2366
2568
    def test_symlink_all_wt(self):
2367
2569
        """Check behavior if all trees are Working Trees."""
2368
 
        self.requireFeature(tests.SymlinkFeature)
 
2570
        self.requireFeature(features.SymlinkFeature)
2369
2571
        # The big issue is that entry.symlink_target is None for WorkingTrees.
2370
2572
        # So we need to make sure we handle that case correctly.
2371
2573
        #   A   foo => bar
2382
2584
        wt.lock_write()
2383
2585
        self.addCleanup(wt.unlock)
2384
2586
        os.symlink('bar', 'path/foo')
2385
 
        wt.add(['foo'], ['foo-id'])
2386
 
        wt.commit('add symlink', rev_id='A-id')
 
2587
        wt.add(['foo'], [b'foo-id'])
 
2588
        wt.commit('add symlink', rev_id=b'A-id')
2387
2589
        os.remove('path/foo')
2388
2590
        os.symlink('baz', 'path/foo')
2389
 
        wt.commit('foo => baz', rev_id='B-id')
2390
 
        wt.set_last_revision('A-id')
2391
 
        wt.branch.set_last_revision_info(1, 'A-id')
 
2591
        wt.commit('foo => baz', rev_id=b'B-id')
 
2592
        wt.set_last_revision(b'A-id')
 
2593
        wt.branch.set_last_revision_info(1, b'A-id')
2392
2594
        wt.revert()
2393
 
        wt.commit('C', rev_id='C-id')
2394
 
        wt.merge_from_branch(wt.branch, 'B-id')
2395
 
        self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2396
 
        wt.commit('E merges C & B', rev_id='E-id')
 
2595
        wt.commit('C', rev_id=b'C-id')
 
2596
        wt.merge_from_branch(wt.branch, b'B-id')
 
2597
        self.assertEqual('baz', wt.get_symlink_target('foo'))
 
2598
        wt.commit('E merges C & B', rev_id=b'E-id')
2397
2599
        os.remove('path/foo')
2398
2600
        os.symlink('bing', 'path/foo')
2399
 
        wt.commit('F foo => bing', rev_id='F-id')
2400
 
        wt.set_last_revision('B-id')
2401
 
        wt.branch.set_last_revision_info(2, 'B-id')
 
2601
        wt.commit('F foo => bing', rev_id=b'F-id')
 
2602
        wt.set_last_revision(b'B-id')
 
2603
        wt.branch.set_last_revision_info(2, b'B-id')
2402
2604
        wt.revert()
2403
 
        wt.merge_from_branch(wt.branch, 'C-id')
2404
 
        wt.commit('D merges B & C', rev_id='D-id')
2405
 
        wt_base = wt.bzrdir.sprout('base', 'A-id').open_workingtree()
 
2605
        wt.merge_from_branch(wt.branch, b'C-id')
 
2606
        wt.commit('D merges B & C', rev_id=b'D-id')
 
2607
        wt_base = wt.controldir.sprout('base', b'A-id').open_workingtree()
2406
2608
        wt_base.lock_read()
2407
2609
        self.addCleanup(wt_base.unlock)
2408
 
        wt_lca1 = wt.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
 
2610
        wt_lca1 = wt.controldir.sprout('b-tree', b'B-id').open_workingtree()
2409
2611
        wt_lca1.lock_read()
2410
2612
        self.addCleanup(wt_lca1.unlock)
2411
 
        wt_lca2 = wt.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
 
2613
        wt_lca2 = wt.controldir.sprout('c-tree', b'C-id').open_workingtree()
2412
2614
        wt_lca2.lock_read()
2413
2615
        self.addCleanup(wt_lca2.unlock)
2414
 
        wt_other = wt.bzrdir.sprout('other', 'F-id').open_workingtree()
 
2616
        wt_other = wt.controldir.sprout('other', b'F-id').open_workingtree()
2415
2617
        wt_other.lock_read()
2416
2618
        self.addCleanup(wt_other.unlock)
2417
2619
        merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2418
 
            wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
 
2620
                                            wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2419
2621
        entries = list(merge_obj._entries_lca())
2420
2622
        root_id = wt.path2id('')
2421
 
        self.assertEqual([('foo-id', True,
 
2623
        self.assertEqual([(b'foo-id', True,
 
2624
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2422
2625
                           ((root_id, [root_id, root_id]), root_id, root_id),
2423
2626
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2424
 
                           ((False, [False, False]), False, False)),
2425
 
                         ], entries)
 
2627
                           ((False, [False, False]), False, False),
 
2628
                           False),
 
2629
                          ], entries)
2426
2630
 
2427
2631
    def test_other_reverted_path_to_base(self):
2428
2632
        #   A       Path at 'foo'
2435
2639
        #     |
2436
2640
        #     F     Path at 'foo'
2437
2641
        builder = self.get_builder()
2438
 
        builder.build_snapshot('A-id', None,
2439
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2440
 
             ('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2441
 
        builder.build_snapshot('C-id', ['A-id'], [])
2442
 
        builder.build_snapshot('B-id', ['A-id'],
2443
 
            [('rename', ('foo', 'bar'))])
2444
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
2445
 
            [('rename', ('foo', 'bar'))]) # merge the rename
2446
 
        builder.build_snapshot('F-id', ['E-id'],
2447
 
            [('rename', ('bar', 'foo'))]) # Rename back to BASE
2448
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2449
 
        wt, conflicts = self.do_merge(builder, 'F-id')
 
2642
        builder.build_snapshot(None,
 
2643
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2644
                                ('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
 
2645
                               revision_id=b'A-id')
 
2646
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2647
        builder.build_snapshot([b'A-id'],
 
2648
                               [('rename', ('foo', 'bar'))], revision_id=b'B-id')
 
2649
        builder.build_snapshot([b'C-id', b'B-id'],
 
2650
                               [('rename', ('foo', 'bar'))], revision_id=b'E-id')  # merge the rename
 
2651
        builder.build_snapshot([b'E-id'],
 
2652
                               [('rename', ('bar', 'foo'))], revision_id=b'F-id')  # Rename back to BASE
 
2653
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2654
        wt, conflicts = self.do_merge(builder, b'F-id')
2450
2655
        self.assertEqual(0, conflicts)
2451
 
        self.assertEqual('foo', wt.id2path('foo-id'))
 
2656
        self.assertEqual('foo', wt.id2path(b'foo-id'))
2452
2657
 
2453
2658
    def test_other_reverted_content_to_base(self):
2454
2659
        builder = self.get_builder()
2455
 
        builder.build_snapshot('A-id', None,
2456
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2457
 
             ('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2458
 
        builder.build_snapshot('C-id', ['A-id'], [])
2459
 
        builder.build_snapshot('B-id', ['A-id'],
2460
 
            [('modify', ('foo-id', 'B content\n'))])
2461
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
2462
 
            [('modify', ('foo-id', 'B content\n'))]) # merge the content
2463
 
        builder.build_snapshot('F-id', ['E-id'],
2464
 
            [('modify', ('foo-id', 'base content\n'))]) # Revert back to BASE
2465
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2466
 
        wt, conflicts = self.do_merge(builder, 'F-id')
 
2660
        builder.build_snapshot(None,
 
2661
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2662
                                ('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
 
2663
                               revision_id=b'A-id')
 
2664
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2665
        builder.build_snapshot([b'A-id'],
 
2666
                               [('modify', ('foo', b'B content\n'))],
 
2667
                               revision_id=b'B-id')
 
2668
        builder.build_snapshot([b'C-id', b'B-id'],
 
2669
                               [('modify', ('foo', b'B content\n'))],
 
2670
                               revision_id=b'E-id')  # merge the content
 
2671
        builder.build_snapshot([b'E-id'],
 
2672
                               [('modify', ('foo', b'base content\n'))],
 
2673
                               revision_id=b'F-id')  # Revert back to BASE
 
2674
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2675
        wt, conflicts = self.do_merge(builder, b'F-id')
2467
2676
        self.assertEqual(0, conflicts)
2468
2677
        # TODO: We need to use the per-file graph to properly select a BASE
2469
2678
        #       before this will work. Or at least use the LCA trees to find
2470
2679
        #       the appropriate content base. (which is B, not A).
2471
 
        self.assertEqual('base content\n', wt.get_file_text('foo-id'))
 
2680
        self.assertEqual(b'base content\n', wt.get_file_text('foo'))
2472
2681
 
2473
2682
    def test_other_modified_content(self):
2474
2683
        builder = self.get_builder()
2475
 
        builder.build_snapshot('A-id', None,
2476
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2477
 
             ('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2478
 
        builder.build_snapshot('C-id', ['A-id'], [])
2479
 
        builder.build_snapshot('B-id', ['A-id'],
2480
 
            [('modify', ('foo-id', 'B content\n'))])
2481
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
2482
 
            [('modify', ('foo-id', 'B content\n'))]) # merge the content
2483
 
        builder.build_snapshot('F-id', ['E-id'],
2484
 
            [('modify', ('foo-id', 'F content\n'))]) # Override B content
2485
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2486
 
        wt, conflicts = self.do_merge(builder, 'F-id')
 
2684
        builder.build_snapshot(None,
 
2685
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2686
                                ('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
 
2687
                               revision_id=b'A-id')
 
2688
        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
 
2689
        builder.build_snapshot([b'A-id'],
 
2690
                               [('modify', ('foo', b'B content\n'))],
 
2691
                               revision_id=b'B-id')
 
2692
        builder.build_snapshot([b'C-id', b'B-id'],
 
2693
                               [('modify', ('foo', b'B content\n'))],
 
2694
                               revision_id=b'E-id')  # merge the content
 
2695
        builder.build_snapshot([b'E-id'],
 
2696
                               [('modify', ('foo', b'F content\n'))],
 
2697
                               revision_id=b'F-id')  # Override B content
 
2698
        builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
 
2699
        wt, conflicts = self.do_merge(builder, b'F-id')
2487
2700
        self.assertEqual(0, conflicts)
2488
 
        self.assertEqual('F content\n', wt.get_file_text('foo-id'))
 
2701
        self.assertEqual(b'F content\n', wt.get_file_text('foo'))
2489
2702
 
2490
2703
    def test_all_wt(self):
2491
2704
        """Check behavior if all trees are Working Trees."""
2498
2711
        #   |X|
2499
2712
        #   D E E updates content, renames 'b' => 'c'
2500
2713
        builder = self.get_builder()
2501
 
        builder.build_snapshot('A-id', None,
2502
 
            [('add', (u'', 'a-root-id', 'directory', None)),
2503
 
             ('add', (u'a', 'a-id', 'file', 'base content\n')),
2504
 
             ('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2505
 
        builder.build_snapshot('B-id', ['A-id'],
2506
 
            [('modify', ('foo-id', 'B content\n'))])
2507
 
        builder.build_snapshot('C-id', ['A-id'],
2508
 
            [('rename', ('a', 'b'))])
2509
 
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
2510
 
            [('rename', ('b', 'c')),
2511
 
             ('modify', ('foo-id', 'E content\n'))])
2512
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
2513
 
            [('rename', ('a', 'b'))]) # merged change
 
2714
        builder.build_snapshot(None,
 
2715
                               [('add', (u'', b'a-root-id', 'directory', None)),
 
2716
                                ('add', (u'a', b'a-id', 'file', b'base content\n')),
 
2717
                                   ('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
 
2718
                               revision_id=b'A-id')
 
2719
        builder.build_snapshot([b'A-id'],
 
2720
                               [('modify', ('foo', b'B content\n'))],
 
2721
                               revision_id=b'B-id')
 
2722
        builder.build_snapshot([b'A-id'],
 
2723
                               [('rename', ('a', 'b'))],
 
2724
                               revision_id=b'C-id')
 
2725
        builder.build_snapshot([b'C-id', b'B-id'],
 
2726
                               [('rename', ('b', 'c')),
 
2727
                                ('modify', ('foo', b'E content\n'))],
 
2728
                               revision_id=b'E-id')
 
2729
        builder.build_snapshot([b'B-id', b'C-id'],
 
2730
                               [('rename', ('a', 'b'))], revision_id=b'D-id')  # merged change
2514
2731
        wt_this = self.get_wt_from_builder(builder)
2515
 
        wt_base = wt_this.bzrdir.sprout('base', 'A-id').open_workingtree()
 
2732
        wt_base = wt_this.controldir.sprout('base', b'A-id').open_workingtree()
2516
2733
        wt_base.lock_read()
2517
2734
        self.addCleanup(wt_base.unlock)
2518
 
        wt_lca1 = wt_this.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
 
2735
        wt_lca1 = wt_this.controldir.sprout(
 
2736
            'b-tree', b'B-id').open_workingtree()
2519
2737
        wt_lca1.lock_read()
2520
2738
        self.addCleanup(wt_lca1.unlock)
2521
 
        wt_lca2 = wt_this.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
 
2739
        wt_lca2 = wt_this.controldir.sprout(
 
2740
            'c-tree', b'C-id').open_workingtree()
2522
2741
        wt_lca2.lock_read()
2523
2742
        self.addCleanup(wt_lca2.unlock)
2524
 
        wt_other = wt_this.bzrdir.sprout('other', 'E-id').open_workingtree()
 
2743
        wt_other = wt_this.controldir.sprout(
 
2744
            'other', b'E-id').open_workingtree()
2525
2745
        wt_other.lock_read()
2526
2746
        self.addCleanup(wt_other.unlock)
2527
2747
        merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2528
 
            wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
 
2748
                                            wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2529
2749
        entries = list(merge_obj._entries_lca())
2530
 
        root_id = 'a-root-id'
2531
 
        self.assertEqual([('a-id', False,
2532
 
                           ((root_id, [root_id, root_id]), root_id, root_id),
2533
 
                           ((u'a', [u'a', u'b']), u'c', u'b'),
2534
 
                           ((False, [False, False]), False, False)),
2535
 
                          ('foo-id', True,
2536
 
                           ((root_id, [root_id, root_id]), root_id, root_id),
2537
 
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2538
 
                           ((False, [False, False]), False, False)),
2539
 
                         ], entries)
 
2750
        root_id = b'a-root-id'
 
2751
        self.assertEqual([(b'a-id', False,
 
2752
                           ((u'a', [u'a', u'b']), u'c', u'b'),
 
2753
                           ((root_id, [root_id, root_id]), root_id, root_id),
 
2754
                           ((u'a', [u'a', u'b']), u'c', u'b'),
 
2755
                           ((False, [False, False]), False, False),
 
2756
                           False),
 
2757
                          (b'foo-id', True,
 
2758
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
 
2759
                           ((root_id, [root_id, root_id]), root_id, root_id),
 
2760
                           ((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
 
2761
                           ((False, [False, False]), False, False),
 
2762
                           False),
 
2763
                          ], entries)
2540
2764
 
2541
2765
    def test_nested_tree_unmodified(self):
2542
2766
        # Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2543
2767
        # 'tree-reference'
2544
2768
        wt = self.make_branch_and_tree('tree',
2545
 
            format='dirstate-with-subtree')
 
2769
                                       format='development-subtree')
2546
2770
        wt.lock_write()
2547
2771
        self.addCleanup(wt.unlock)
2548
2772
        sub_tree = self.make_branch_and_tree('tree/sub-tree',
2549
 
            format='dirstate-with-subtree')
2550
 
        wt.set_root_id('a-root-id')
2551
 
        sub_tree.set_root_id('sub-tree-root')
2552
 
        self.build_tree_contents([('tree/sub-tree/file', 'text1')])
 
2773
                                             format='development-subtree')
 
2774
        wt.set_root_id(b'a-root-id')
 
2775
        sub_tree.set_root_id(b'sub-tree-root')
 
2776
        self.build_tree_contents([('tree/sub-tree/file', b'text1')])
2553
2777
        sub_tree.add('file')
2554
 
        sub_tree.commit('foo', rev_id='sub-A-id')
 
2778
        sub_tree.commit('foo', rev_id=b'sub-A-id')
2555
2779
        wt.add_reference(sub_tree)
2556
 
        wt.commit('set text to 1', rev_id='A-id', recursive=None)
 
2780
        wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2557
2781
        # Now create a criss-cross merge in the parent, without modifying the
2558
2782
        # subtree
2559
 
        wt.commit('B', rev_id='B-id', recursive=None)
2560
 
        wt.set_last_revision('A-id')
2561
 
        wt.branch.set_last_revision_info(1, 'A-id')
2562
 
        wt.commit('C', rev_id='C-id', recursive=None)
2563
 
        wt.merge_from_branch(wt.branch, to_revision='B-id')
2564
 
        wt.commit('E', rev_id='E-id', recursive=None)
2565
 
        wt.set_parent_ids(['B-id', 'C-id'])
2566
 
        wt.branch.set_last_revision_info(2, 'B-id')
2567
 
        wt.commit('D', rev_id='D-id', recursive=None)
 
2783
        wt.commit('B', rev_id=b'B-id', recursive=None)
 
2784
        wt.set_last_revision(b'A-id')
 
2785
        wt.branch.set_last_revision_info(1, b'A-id')
 
2786
        wt.commit('C', rev_id=b'C-id', recursive=None)
 
2787
        wt.merge_from_branch(wt.branch, to_revision=b'B-id')
 
2788
        wt.commit('E', rev_id=b'E-id', recursive=None)
 
2789
        wt.set_parent_ids([b'B-id', b'C-id'])
 
2790
        wt.branch.set_last_revision_info(2, b'B-id')
 
2791
        wt.commit('D', rev_id=b'D-id', recursive=None)
2568
2792
 
2569
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2570
 
            wt, 'E-id')
 
2793
        merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2571
2794
        merger.merge_type = _mod_merge.Merge3Merger
2572
2795
        merge_obj = merger.make_merger()
2573
2796
        entries = list(merge_obj._entries_lca())
2577
2800
        # Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2578
2801
        # 'tree-reference'
2579
2802
        wt = self.make_branch_and_tree('tree',
2580
 
            format='dirstate-with-subtree')
 
2803
                                       format='development-subtree')
2581
2804
        wt.lock_write()
2582
2805
        self.addCleanup(wt.unlock)
2583
2806
        sub_tree = self.make_branch_and_tree('tree/sub',
2584
 
            format='dirstate-with-subtree')
2585
 
        wt.set_root_id('a-root-id')
2586
 
        sub_tree.set_root_id('sub-tree-root')
2587
 
        self.build_tree_contents([('tree/sub/file', 'text1')])
 
2807
                                             format='development-subtree')
 
2808
        wt.set_root_id(b'a-root-id')
 
2809
        sub_tree.set_root_id(b'sub-tree-root')
 
2810
        self.build_tree_contents([('tree/sub/file', b'text1')])
2588
2811
        sub_tree.add('file')
2589
 
        sub_tree.commit('foo', rev_id='sub-A-id')
 
2812
        sub_tree.commit('foo', rev_id=b'sub-A-id')
2590
2813
        wt.add_reference(sub_tree)
2591
 
        wt.commit('set text to 1', rev_id='A-id', recursive=None)
 
2814
        wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2592
2815
        # Now create a criss-cross merge in the parent, without modifying the
2593
2816
        # subtree
2594
 
        wt.commit('B', rev_id='B-id', recursive=None)
2595
 
        wt.set_last_revision('A-id')
2596
 
        wt.branch.set_last_revision_info(1, 'A-id')
2597
 
        wt.commit('C', rev_id='C-id', recursive=None)
2598
 
        wt.merge_from_branch(wt.branch, to_revision='B-id')
2599
 
        self.build_tree_contents([('tree/sub/file', 'text2')])
2600
 
        sub_tree.commit('modify contents', rev_id='sub-B-id')
2601
 
        wt.commit('E', rev_id='E-id', recursive=None)
2602
 
        wt.set_parent_ids(['B-id', 'C-id'])
2603
 
        wt.branch.set_last_revision_info(2, 'B-id')
2604
 
        wt.commit('D', rev_id='D-id', recursive=None)
 
2817
        wt.commit('B', rev_id=b'B-id', recursive=None)
 
2818
        wt.set_last_revision(b'A-id')
 
2819
        wt.branch.set_last_revision_info(1, b'A-id')
 
2820
        wt.commit('C', rev_id=b'C-id', recursive=None)
 
2821
        wt.merge_from_branch(wt.branch, to_revision=b'B-id')
 
2822
        self.build_tree_contents([('tree/sub/file', b'text2')])
 
2823
        sub_tree.commit('modify contents', rev_id=b'sub-B-id')
 
2824
        wt.commit('E', rev_id=b'E-id', recursive=None)
 
2825
        wt.set_parent_ids([b'B-id', b'C-id'])
 
2826
        wt.branch.set_last_revision_info(2, b'B-id')
 
2827
        wt.commit('D', rev_id=b'D-id', recursive=None)
2605
2828
 
2606
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2607
 
            wt, 'E-id')
 
2829
        merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2608
2830
        merger.merge_type = _mod_merge.Merge3Merger
2609
2831
        merge_obj = merger.make_merger()
2610
2832
        entries = list(merge_obj._entries_lca())
2616
2838
        # Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2617
2839
        # 'tree-reference'
2618
2840
        wt = self.make_branch_and_tree('tree',
2619
 
            format='dirstate-with-subtree')
 
2841
                                       format='development-subtree')
2620
2842
        wt.lock_write()
2621
2843
        self.addCleanup(wt.unlock)
2622
2844
        sub_tree = self.make_branch_and_tree('tree/sub',
2623
 
            format='dirstate-with-subtree')
2624
 
        wt.set_root_id('a-root-id')
2625
 
        sub_tree.set_root_id('sub-tree-root')
2626
 
        self.build_tree_contents([('tree/sub/file', 'text1')])
 
2845
                                             format='development-subtree')
 
2846
        wt.set_root_id(b'a-root-id')
 
2847
        sub_tree.set_root_id(b'sub-tree-root')
 
2848
        self.build_tree_contents([('tree/sub/file', b'text1')])
2627
2849
        sub_tree.add('file')
2628
 
        sub_tree.commit('foo', rev_id='sub-A-id')
 
2850
        sub_tree.commit('foo', rev_id=b'sub-A-id')
2629
2851
        wt.add_reference(sub_tree)
2630
 
        wt.commit('set text to 1', rev_id='A-id', recursive=None)
 
2852
        wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2631
2853
        # Now create a criss-cross merge in the parent, without modifying the
2632
2854
        # subtree
2633
 
        wt.commit('B', rev_id='B-id', recursive=None)
2634
 
        wt.set_last_revision('A-id')
2635
 
        wt.branch.set_last_revision_info(1, 'A-id')
2636
 
        wt.commit('C', rev_id='C-id', recursive=None)
2637
 
        wt.merge_from_branch(wt.branch, to_revision='B-id')
 
2855
        wt.commit('B', rev_id=b'B-id', recursive=None)
 
2856
        wt.set_last_revision(b'A-id')
 
2857
        wt.branch.set_last_revision_info(1, b'A-id')
 
2858
        wt.commit('C', rev_id=b'C-id', recursive=None)
 
2859
        wt.merge_from_branch(wt.branch, to_revision=b'B-id')
2638
2860
        wt.rename_one('sub', 'alt_sub')
2639
 
        wt.commit('E', rev_id='E-id', recursive=None)
2640
 
        wt.set_last_revision('B-id')
 
2861
        wt.commit('E', rev_id=b'E-id', recursive=None)
 
2862
        wt.set_last_revision(b'B-id')
2641
2863
        wt.revert()
2642
 
        wt.set_parent_ids(['B-id', 'C-id'])
2643
 
        wt.branch.set_last_revision_info(2, 'B-id')
2644
 
        wt.commit('D', rev_id='D-id', recursive=None)
 
2864
        wt.set_parent_ids([b'B-id', b'C-id'])
 
2865
        wt.branch.set_last_revision_info(2, b'B-id')
 
2866
        wt.commit('D', rev_id=b'D-id', recursive=None)
2645
2867
 
2646
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2647
 
            wt, 'E-id')
 
2868
        merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2648
2869
        merger.merge_type = _mod_merge.Merge3Merger
2649
2870
        merge_obj = merger.make_merger()
2650
2871
        entries = list(merge_obj._entries_lca())
2651
 
        root_id = 'a-root-id'
2652
 
        self.assertEqual([('sub-tree-root', False,
 
2872
        root_id = b'a-root-id'
 
2873
        self.assertEqual([(b'sub-tree-root', False,
 
2874
                           ((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2653
2875
                           ((root_id, [root_id, root_id]), root_id, root_id),
2654
2876
                           ((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2655
 
                           ((False, [False, False]), False, False)),
2656
 
                         ], entries)
 
2877
                           ((False, [False, False]), False, False),
 
2878
                           False),
 
2879
                          ], entries)
2657
2880
 
2658
2881
    def test_nested_tree_subtree_renamed_and_modified(self):
2659
2882
        # Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2660
2883
        # 'tree-reference'
2661
2884
        wt = self.make_branch_and_tree('tree',
2662
 
            format='dirstate-with-subtree')
 
2885
                                       format='development-subtree')
2663
2886
        wt.lock_write()
2664
2887
        self.addCleanup(wt.unlock)
2665
2888
        sub_tree = self.make_branch_and_tree('tree/sub',
2666
 
            format='dirstate-with-subtree')
2667
 
        wt.set_root_id('a-root-id')
2668
 
        sub_tree.set_root_id('sub-tree-root')
2669
 
        self.build_tree_contents([('tree/sub/file', 'text1')])
 
2889
                                             format='development-subtree')
 
2890
        wt.set_root_id(b'a-root-id')
 
2891
        sub_tree.set_root_id(b'sub-tree-root')
 
2892
        self.build_tree_contents([('tree/sub/file', b'text1')])
2670
2893
        sub_tree.add('file')
2671
 
        sub_tree.commit('foo', rev_id='sub-A-id')
 
2894
        sub_tree.commit('foo', rev_id=b'sub-A-id')
2672
2895
        wt.add_reference(sub_tree)
2673
 
        wt.commit('set text to 1', rev_id='A-id', recursive=None)
 
2896
        wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2674
2897
        # Now create a criss-cross merge in the parent, without modifying the
2675
2898
        # subtree
2676
 
        wt.commit('B', rev_id='B-id', recursive=None)
2677
 
        wt.set_last_revision('A-id')
2678
 
        wt.branch.set_last_revision_info(1, 'A-id')
2679
 
        wt.commit('C', rev_id='C-id', recursive=None)
2680
 
        wt.merge_from_branch(wt.branch, to_revision='B-id')
2681
 
        self.build_tree_contents([('tree/sub/file', 'text2')])
2682
 
        sub_tree.commit('modify contents', rev_id='sub-B-id')
 
2899
        wt.commit('B', rev_id=b'B-id', recursive=None)
 
2900
        wt.set_last_revision(b'A-id')
 
2901
        wt.branch.set_last_revision_info(1, b'A-id')
 
2902
        wt.commit('C', rev_id=b'C-id', recursive=None)
 
2903
        wt.merge_from_branch(wt.branch, to_revision=b'B-id')
 
2904
        self.build_tree_contents([('tree/sub/file', b'text2')])
 
2905
        sub_tree.commit('modify contents', rev_id=b'sub-B-id')
2683
2906
        wt.rename_one('sub', 'alt_sub')
2684
 
        wt.commit('E', rev_id='E-id', recursive=None)
2685
 
        wt.set_last_revision('B-id')
 
2907
        wt.commit('E', rev_id=b'E-id', recursive=None)
 
2908
        wt.set_last_revision(b'B-id')
2686
2909
        wt.revert()
2687
 
        wt.set_parent_ids(['B-id', 'C-id'])
2688
 
        wt.branch.set_last_revision_info(2, 'B-id')
2689
 
        wt.commit('D', rev_id='D-id', recursive=None)
 
2910
        wt.set_parent_ids([b'B-id', b'C-id'])
 
2911
        wt.branch.set_last_revision_info(2, b'B-id')
 
2912
        wt.commit('D', rev_id=b'D-id', recursive=None)
2690
2913
 
2691
 
        merger = _mod_merge.Merger.from_revision_ids(None,
2692
 
            wt, 'E-id')
 
2914
        merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2693
2915
        merger.merge_type = _mod_merge.Merge3Merger
2694
2916
        merge_obj = merger.make_merger()
2695
2917
        entries = list(merge_obj._entries_lca())
2696
 
        root_id = 'a-root-id'
2697
 
        self.assertEqual([('sub-tree-root', False,
 
2918
        root_id = b'a-root-id'
 
2919
        self.assertEqual([(b'sub-tree-root', False,
 
2920
                           ((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2698
2921
                           ((root_id, [root_id, root_id]), root_id, root_id),
2699
2922
                           ((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2700
 
                           ((False, [False, False]), False, False)),
2701
 
                         ], entries)
 
2923
                           ((False, [False, False]), False, False),
 
2924
                           False),
 
2925
                          ], entries)
2702
2926
 
2703
2927
 
2704
2928
class TestLCAMultiWay(tests.TestCase):
2706
2930
    def assertLCAMultiWay(self, expected, base, lcas, other, this,
2707
2931
                          allow_overriding_lca=True):
2708
2932
        self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2709
 
                                (base, lcas), other, this,
2710
 
                                allow_overriding_lca=allow_overriding_lca))
 
2933
            (base, lcas), other, this,
 
2934
            allow_overriding_lca=allow_overriding_lca))
2711
2935
 
2712
2936
    def test_other_equal_equal_lcas(self):
2713
2937
        """Test when OTHER=LCA and all LCAs are identical."""
2714
2938
        self.assertLCAMultiWay('this',
2715
 
            'bval', ['bval', 'bval'], 'bval', 'bval')
2716
 
        self.assertLCAMultiWay('this',
2717
 
            'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2718
 
        self.assertLCAMultiWay('this',
2719
 
            'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2720
 
        self.assertLCAMultiWay('this',
2721
 
            'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2722
 
        self.assertLCAMultiWay('this',
2723
 
            'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
 
2939
                               'bval', ['bval', 'bval'], 'bval', 'bval')
 
2940
        self.assertLCAMultiWay('this',
 
2941
                               'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
 
2942
        self.assertLCAMultiWay('this',
 
2943
                               'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
 
2944
        self.assertLCAMultiWay('this',
 
2945
                               'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
 
2946
        self.assertLCAMultiWay('this',
 
2947
                               'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2724
2948
 
2725
2949
    def test_other_equal_this(self):
2726
2950
        """Test when other and this are identical."""
2727
2951
        self.assertLCAMultiWay('this',
2728
 
            'bval', ['bval', 'bval'], 'oval', 'oval')
2729
 
        self.assertLCAMultiWay('this',
2730
 
            'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2731
 
        self.assertLCAMultiWay('this',
2732
 
            'bval', ['cval', 'dval'], 'oval', 'oval')
2733
 
        self.assertLCAMultiWay('this',
2734
 
            'bval', [None, 'lcaval'], 'oval', 'oval')
2735
 
        self.assertLCAMultiWay('this',
2736
 
            None, [None, 'lcaval'], 'oval', 'oval')
2737
 
        self.assertLCAMultiWay('this',
2738
 
            None, ['lcaval', 'lcaval'], 'oval', 'oval')
2739
 
        self.assertLCAMultiWay('this',
2740
 
            None, ['cval', 'dval'], 'oval', 'oval')
2741
 
        self.assertLCAMultiWay('this',
2742
 
            None, ['cval', 'dval'], None, None)
2743
 
        self.assertLCAMultiWay('this',
2744
 
            None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
 
2952
                               'bval', ['bval', 'bval'], 'oval', 'oval')
 
2953
        self.assertLCAMultiWay('this',
 
2954
                               'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
 
2955
        self.assertLCAMultiWay('this',
 
2956
                               'bval', ['cval', 'dval'], 'oval', 'oval')
 
2957
        self.assertLCAMultiWay('this',
 
2958
                               'bval', [None, 'lcaval'], 'oval', 'oval')
 
2959
        self.assertLCAMultiWay('this',
 
2960
                               None, [None, 'lcaval'], 'oval', 'oval')
 
2961
        self.assertLCAMultiWay('this',
 
2962
                               None, ['lcaval', 'lcaval'], 'oval', 'oval')
 
2963
        self.assertLCAMultiWay('this',
 
2964
                               None, ['cval', 'dval'], 'oval', 'oval')
 
2965
        self.assertLCAMultiWay('this',
 
2966
                               None, ['cval', 'dval'], None, None)
 
2967
        self.assertLCAMultiWay('this',
 
2968
                               None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2745
2969
 
2746
2970
    def test_no_lcas(self):
2747
2971
        self.assertLCAMultiWay('this',
2748
 
            'bval', [], 'bval', 'tval')
 
2972
                               'bval', [], 'bval', 'tval')
2749
2973
        self.assertLCAMultiWay('other',
2750
 
            'bval', [], 'oval', 'bval')
 
2974
                               'bval', [], 'oval', 'bval')
2751
2975
        self.assertLCAMultiWay('conflict',
2752
 
            'bval', [], 'oval', 'tval')
 
2976
                               'bval', [], 'oval', 'tval')
2753
2977
        self.assertLCAMultiWay('this',
2754
 
            'bval', [], 'oval', 'oval')
 
2978
                               'bval', [], 'oval', 'oval')
2755
2979
 
2756
2980
    def test_lca_supersedes_other_lca(self):
2757
2981
        """If one lca == base, the other lca takes precedence"""
2758
2982
        self.assertLCAMultiWay('this',
2759
 
            'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
 
2983
                               'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2760
2984
        self.assertLCAMultiWay('this',
2761
 
            'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
 
2985
                               'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2762
2986
        # This is actually considered a 'revert' because the 'lcaval' in LCAS
2763
2987
        # supersedes the BASE val (in the other LCA) but then OTHER reverts it
2764
2988
        # back to bval.
2765
2989
        self.assertLCAMultiWay('other',
2766
 
            'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
 
2990
                               'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2767
2991
        self.assertLCAMultiWay('conflict',
2768
 
            'bval', ['bval', 'lcaval'], 'bval', 'tval')
 
2992
                               'bval', ['bval', 'lcaval'], 'bval', 'tval')
2769
2993
 
2770
2994
    def test_other_and_this_pick_different_lca(self):
2771
2995
        # OTHER and THIS resolve the lca conflict in different ways
2772
2996
        self.assertLCAMultiWay('conflict',
2773
 
            'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2774
 
        self.assertLCAMultiWay('conflict',
2775
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
2776
 
        self.assertLCAMultiWay('conflict',
2777
 
            'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
 
2997
                               'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
 
2998
        self.assertLCAMultiWay('conflict',
 
2999
                               'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
 
3000
        self.assertLCAMultiWay('conflict',
 
3001
                               'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
2778
3002
 
2779
3003
    def test_other_in_lca(self):
2780
3004
        # OTHER takes a value of one of the LCAs, THIS takes a new value, which
2781
3005
        # theoretically supersedes both LCA values and 'wins'
2782
 
        self.assertLCAMultiWay('this',
2783
 
            'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
2784
 
        self.assertLCAMultiWay('this',
2785
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval')
2786
 
        self.assertLCAMultiWay('conflict',
2787
 
            'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval',
2788
 
            allow_overriding_lca=False)
2789
 
        self.assertLCAMultiWay('conflict',
2790
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval',
2791
 
            allow_overriding_lca=False)
 
3006
        self.assertLCAMultiWay(
 
3007
            'this', 'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
 
3008
        self.assertLCAMultiWay(
 
3009
            'this', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val',
 
3010
            'newval')
 
3011
        self.assertLCAMultiWay('conflict',
 
3012
                               'bval', ['lca1val',
 
3013
                                        'lca2val'], 'lca1val', 'newval',
 
3014
                               allow_overriding_lca=False)
 
3015
        self.assertLCAMultiWay('conflict',
 
3016
                               'bval', ['lca1val', 'lca2val',
 
3017
                                        'lca3val'], 'lca1val', 'newval',
 
3018
                               allow_overriding_lca=False)
2792
3019
        # THIS reverted back to BASE, but that is an explicit supersede of all
2793
3020
        # LCAs
2794
 
        self.assertLCAMultiWay('this',
2795
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval')
2796
 
        self.assertLCAMultiWay('this',
2797
 
            'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
2798
 
        self.assertLCAMultiWay('conflict',
2799
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval',
2800
 
            allow_overriding_lca=False)
2801
 
        self.assertLCAMultiWay('conflict',
2802
 
            'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval',
2803
 
            allow_overriding_lca=False)
 
3021
        self.assertLCAMultiWay(
 
3022
            'this', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val',
 
3023
            'bval')
 
3024
        self.assertLCAMultiWay(
 
3025
            'this', 'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
 
3026
        self.assertLCAMultiWay('conflict',
 
3027
                               'bval', ['lca1val', 'lca2val',
 
3028
                                        'lca3val'], 'lca1val', 'bval',
 
3029
                               allow_overriding_lca=False)
 
3030
        self.assertLCAMultiWay('conflict',
 
3031
                               'bval', ['lca1val', 'lca2val',
 
3032
                                        'bval'], 'lca1val', 'bval',
 
3033
                               allow_overriding_lca=False)
2804
3034
 
2805
3035
    def test_this_in_lca(self):
2806
3036
        # THIS takes a value of one of the LCAs, OTHER takes a new value, which
2807
3037
        # theoretically supersedes both LCA values and 'wins'
2808
 
        self.assertLCAMultiWay('other',
2809
 
            'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
2810
 
        self.assertLCAMultiWay('other',
2811
 
            'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
2812
 
        self.assertLCAMultiWay('conflict',
2813
 
            'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val',
2814
 
            allow_overriding_lca=False)
2815
 
        self.assertLCAMultiWay('conflict',
2816
 
            'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val',
2817
 
            allow_overriding_lca=False)
 
3038
        self.assertLCAMultiWay(
 
3039
            'other', 'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
 
3040
        self.assertLCAMultiWay(
 
3041
            'other', 'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
 
3042
        self.assertLCAMultiWay('conflict',
 
3043
                               'bval', ['lca1val',
 
3044
                                        'lca2val'], 'oval', 'lca1val',
 
3045
                               allow_overriding_lca=False)
 
3046
        self.assertLCAMultiWay('conflict',
 
3047
                               'bval', ['lca1val',
 
3048
                                        'lca2val'], 'oval', 'lca2val',
 
3049
                               allow_overriding_lca=False)
2818
3050
        # OTHER reverted back to BASE, but that is an explicit supersede of all
2819
3051
        # LCAs
2820
 
        self.assertLCAMultiWay('other',
2821
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val')
2822
 
        self.assertLCAMultiWay('conflict',
2823
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val',
2824
 
            allow_overriding_lca=False)
 
3052
        self.assertLCAMultiWay(
 
3053
            'other', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval',
 
3054
            'lca3val')
 
3055
        self.assertLCAMultiWay(
 
3056
            'conflict', 'bval', ['lca1val', 'lca2val', 'lca3val'],
 
3057
            'bval', 'lca3val', allow_overriding_lca=False)
2825
3058
 
2826
3059
    def test_all_differ(self):
2827
 
        self.assertLCAMultiWay('conflict',
2828
 
            'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
2829
 
        self.assertLCAMultiWay('conflict',
2830
 
            'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2831
 
        self.assertLCAMultiWay('conflict',
2832
 
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
 
3060
        self.assertLCAMultiWay(
 
3061
            'conflict', 'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
 
3062
        self.assertLCAMultiWay(
 
3063
            'conflict', 'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval',
 
3064
            'tval')
 
3065
        self.assertLCAMultiWay(
 
3066
            'conflict', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval',
 
3067
            'tval')
2833
3068
 
2834
3069
 
2835
3070
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2840
3075
 
2841
3076
    def get_merger_factory(self):
2842
3077
        # Allows  the inner methods to access the test attributes
2843
 
        test = self
 
3078
        calls = self.calls
2844
3079
 
2845
3080
        class FooMerger(_mod_merge.ConfigurableFileMerger):
2846
3081
            name_prefix = "foo"
2847
3082
            default_files = ['bar']
2848
3083
 
2849
3084
            def merge_text(self, params):
2850
 
                test.calls.append('merge_text')
 
3085
                calls.append('merge_text')
2851
3086
                return ('not_applicable', None)
2852
3087
 
2853
3088
        def factory(merger):
2873
3108
        factory = self.get_merger_factory()
2874
3109
        self._install_hook(factory)
2875
3110
        builder = self.make_builder()
2876
 
        builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2877
 
        builder.change_contents('bar-id', other='text4', this='text3')
 
3111
        builder.add_file(b'bar-id', builder.tree_root,
 
3112
                         file_name, b'text1', True)
 
3113
        builder.change_contents(b'bar-id', other=b'text4', this=b'text3')
2878
3114
        return builder
2879
3115
 
2880
3116
    def make_kind_change(self):
2881
3117
        factory = self.get_merger_factory()
2882
3118
        self._install_hook(factory)
2883
3119
        builder = self.make_builder()
2884
 
        builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
 
3120
        builder.add_file(b'bar-id', builder.tree_root, 'bar', b'text1', True,
2885
3121
                         this=False)
2886
 
        builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
 
3122
        builder.add_dir(b'bar-dir', builder.tree_root, 'bar-id',
2887
3123
                        base=False, other=False)
2888
3124
        return builder
2889
3125
 
2890
3126
    def test_uses_this_branch(self):
2891
3127
        builder = self.make_text_conflict()
2892
 
        tt = builder.make_preview_transform()
2893
 
        self.addCleanup(tt.finalize)
 
3128
        with builder.make_preview_transform() as tt:
 
3129
            pass
2894
3130
 
2895
3131
    def test_affected_files_cached(self):
2896
3132
        """Ensures that the config variable is cached"""
2902
3138
 
2903
3139
    def test_hook_called_for_text_conflicts(self):
2904
3140
        builder = self.make_text_conflict()
2905
 
        conflicts = builder.merge()
 
3141
        builder.merge()
2906
3142
        # The hook should call the merge_text() method
2907
3143
        self.assertEqual(['merge_text'], self.calls)
2908
3144
 
2909
3145
    def test_hook_not_called_for_kind_change(self):
2910
3146
        builder = self.make_kind_change()
2911
 
        conflicts = builder.merge()
 
3147
        builder.merge()
2912
3148
        # The hook should not call the merge_text() method
2913
3149
        self.assertEqual([], self.calls)
2914
3150
 
2915
3151
    def test_hook_not_called_for_other_files(self):
2916
3152
        builder = self.make_text_conflict('foobar')
2917
 
        conflicts = builder.merge()
 
3153
        builder.merge()
2918
3154
        # The hook should not call the merge_text() method
2919
3155
        self.assertEqual([], self.calls)
 
3156
 
 
3157
 
 
3158
class TestMergeIntoBase(tests.TestCaseWithTransport):
 
3159
 
 
3160
    def setup_simple_branch(self, relpath, shape=None, root_id=None):
 
3161
        """One commit, containing tree specified by optional shape.
 
3162
 
 
3163
        Default is empty tree (just root entry).
 
3164
        """
 
3165
        if root_id is None:
 
3166
            root_id = b'%s-root-id' % (relpath.encode('ascii'),)
 
3167
        wt = self.make_branch_and_tree(relpath)
 
3168
        wt.set_root_id(root_id)
 
3169
        if shape is not None:
 
3170
            adjusted_shape = [relpath + '/' + elem for elem in shape]
 
3171
            self.build_tree(adjusted_shape)
 
3172
            ids = [
 
3173
                (b'%s-%s-id' % (relpath.encode('utf-8'),
 
3174
                                basename(elem.rstrip('/')).encode('ascii')))
 
3175
                for elem in shape]
 
3176
            wt.add(shape, ids=ids)
 
3177
        rev_id = b'r1-%s' % (relpath.encode('utf-8'),)
 
3178
        wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
 
3179
        self.assertEqual(root_id, wt.path2id(''))
 
3180
        return wt
 
3181
 
 
3182
    def setup_two_branches(self, custom_root_ids=True):
 
3183
        """Setup 2 branches, one will be a library, the other a project."""
 
3184
        if custom_root_ids:
 
3185
            root_id = None
 
3186
        else:
 
3187
            root_id = inventory.ROOT_ID
 
3188
        project_wt = self.setup_simple_branch(
 
3189
            'project', ['README', 'dir/', 'dir/file.c'],
 
3190
            root_id)
 
3191
        lib_wt = self.setup_simple_branch(
 
3192
            'lib1', ['README', 'Makefile', 'foo.c'], root_id)
 
3193
 
 
3194
        return project_wt, lib_wt
 
3195
 
 
3196
    def do_merge_into(self, location, merge_as):
 
3197
        """Helper for using MergeIntoMerger.
 
3198
 
 
3199
        :param location: location of directory to merge from, either the
 
3200
            location of a branch or of a path inside a branch.
 
3201
        :param merge_as: the path in a tree to add the new directory as.
 
3202
        :returns: the conflicts from 'do_merge'.
 
3203
        """
 
3204
        with contextlib.ExitStack() as stack:
 
3205
            # Open and lock the various tree and branch objects
 
3206
            wt, subdir_relpath = WorkingTree.open_containing(merge_as)
 
3207
            stack.enter_context(wt.lock_write())
 
3208
            branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
 
3209
                location)
 
3210
            stack.enter_context(branch_to_merge.lock_read())
 
3211
            other_tree = branch_to_merge.basis_tree()
 
3212
            stack.enter_context(other_tree.lock_read())
 
3213
            # Perform the merge
 
3214
            merger = _mod_merge.MergeIntoMerger(
 
3215
                this_tree=wt, other_tree=other_tree, other_branch=branch_to_merge,
 
3216
                target_subdir=subdir_relpath, source_subpath=subdir_to_merge)
 
3217
            merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
 
3218
            conflicts = merger.do_merge()
 
3219
            merger.set_pending()
 
3220
            return conflicts
 
3221
 
 
3222
    def assertTreeEntriesEqual(self, expected_entries, tree):
 
3223
        """Assert that 'tree' contains the expected inventory entries.
 
3224
 
 
3225
        :param expected_entries: sequence of (path, file-id) pairs.
 
3226
        """
 
3227
        files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
 
3228
        self.assertEqual(expected_entries, files)
 
3229
 
 
3230
 
 
3231
class TestMergeInto(TestMergeIntoBase):
 
3232
 
 
3233
    def test_newdir_with_unique_roots(self):
 
3234
        """Merge a branch with a unique root into a new directory."""
 
3235
        project_wt, lib_wt = self.setup_two_branches()
 
3236
        self.do_merge_into('lib1', 'project/lib1')
 
3237
        project_wt.lock_read()
 
3238
        self.addCleanup(project_wt.unlock)
 
3239
        # The r1-lib1 revision should be merged into this one
 
3240
        self.assertEqual([b'r1-project', b'r1-lib1'],
 
3241
                         project_wt.get_parent_ids())
 
3242
        self.assertTreeEntriesEqual(
 
3243
            [('', b'project-root-id'),
 
3244
             ('README', b'project-README-id'),
 
3245
             ('dir', b'project-dir-id'),
 
3246
             ('lib1', b'lib1-root-id'),
 
3247
             ('dir/file.c', b'project-file.c-id'),
 
3248
             ('lib1/Makefile', b'lib1-Makefile-id'),
 
3249
             ('lib1/README', b'lib1-README-id'),
 
3250
             ('lib1/foo.c', b'lib1-foo.c-id'),
 
3251
             ], project_wt)
 
3252
 
 
3253
    def test_subdir(self):
 
3254
        """Merge a branch into a subdirectory of an existing directory."""
 
3255
        project_wt, lib_wt = self.setup_two_branches()
 
3256
        self.do_merge_into('lib1', 'project/dir/lib1')
 
3257
        project_wt.lock_read()
 
3258
        self.addCleanup(project_wt.unlock)
 
3259
        # The r1-lib1 revision should be merged into this one
 
3260
        self.assertEqual([b'r1-project', b'r1-lib1'],
 
3261
                         project_wt.get_parent_ids())
 
3262
        self.assertTreeEntriesEqual(
 
3263
            [('', b'project-root-id'),
 
3264
             ('README', b'project-README-id'),
 
3265
             ('dir', b'project-dir-id'),
 
3266
             ('dir/file.c', b'project-file.c-id'),
 
3267
             ('dir/lib1', b'lib1-root-id'),
 
3268
             ('dir/lib1/Makefile', b'lib1-Makefile-id'),
 
3269
             ('dir/lib1/README', b'lib1-README-id'),
 
3270
             ('dir/lib1/foo.c', b'lib1-foo.c-id'),
 
3271
             ], project_wt)
 
3272
 
 
3273
    def test_newdir_with_repeat_roots(self):
 
3274
        """If the file-id of the dir to be merged already exists a new ID will
 
3275
        be allocated to let the merge happen.
 
3276
        """
 
3277
        project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
 
3278
        root_id = project_wt.path2id('')
 
3279
        self.do_merge_into('lib1', 'project/lib1')
 
3280
        project_wt.lock_read()
 
3281
        self.addCleanup(project_wt.unlock)
 
3282
        # The r1-lib1 revision should be merged into this one
 
3283
        self.assertEqual([b'r1-project', b'r1-lib1'],
 
3284
                         project_wt.get_parent_ids())
 
3285
        new_lib1_id = project_wt.path2id('lib1')
 
3286
        self.assertNotEqual(None, new_lib1_id)
 
3287
        self.assertTreeEntriesEqual(
 
3288
            [('', root_id),
 
3289
             ('README', b'project-README-id'),
 
3290
             ('dir', b'project-dir-id'),
 
3291
             ('lib1', new_lib1_id),
 
3292
             ('dir/file.c', b'project-file.c-id'),
 
3293
             ('lib1/Makefile', b'lib1-Makefile-id'),
 
3294
             ('lib1/README', b'lib1-README-id'),
 
3295
             ('lib1/foo.c', b'lib1-foo.c-id'),
 
3296
             ], project_wt)
 
3297
 
 
3298
    def test_name_conflict(self):
 
3299
        """When the target directory name already exists a conflict is
 
3300
        generated and the original directory is renamed to foo.moved.
 
3301
        """
 
3302
        dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
 
3303
        self.setup_simple_branch('src', ['README'])
 
3304
        conflicts = self.do_merge_into('src', 'dest/dir')
 
3305
        self.assertEqual(1, conflicts)
 
3306
        dest_wt.lock_read()
 
3307
        self.addCleanup(dest_wt.unlock)
 
3308
        # The r1-lib1 revision should be merged into this one
 
3309
        self.assertEqual([b'r1-dest', b'r1-src'], dest_wt.get_parent_ids())
 
3310
        self.assertTreeEntriesEqual(
 
3311
            [('', b'dest-root-id'),
 
3312
             ('dir', b'src-root-id'),
 
3313
             ('dir.moved', b'dest-dir-id'),
 
3314
             ('dir/README', b'src-README-id'),
 
3315
             ('dir.moved/file.txt', b'dest-file.txt-id'),
 
3316
             ], dest_wt)
 
3317
 
 
3318
    def test_file_id_conflict(self):
 
3319
        """A conflict is generated if the merge-into adds a file (or other
 
3320
        inventory entry) with a file-id that already exists in the target tree.
 
3321
        """
 
3322
        self.setup_simple_branch('dest', ['file.txt'])
 
3323
        # Make a second tree with a file-id that will clash with file.txt in
 
3324
        # dest.
 
3325
        src_wt = self.make_branch_and_tree('src')
 
3326
        self.build_tree(['src/README'])
 
3327
        src_wt.add(['README'], ids=[b'dest-file.txt-id'])
 
3328
        src_wt.commit("Rev 1 of src.", rev_id=b'r1-src')
 
3329
        conflicts = self.do_merge_into('src', 'dest/dir')
 
3330
        # This is an edge case that shouldn't happen to users very often.  So
 
3331
        # we don't care really about the exact presentation of the conflict,
 
3332
        # just that there is one.
 
3333
        self.assertEqual(1, conflicts)
 
3334
 
 
3335
    def test_only_subdir(self):
 
3336
        """When the location points to just part of a tree, merge just that
 
3337
        subtree.
 
3338
        """
 
3339
        dest_wt = self.setup_simple_branch('dest')
 
3340
        self.setup_simple_branch('src', ['hello.txt', 'dir/', 'dir/foo.c'])
 
3341
        self.do_merge_into('src/dir', 'dest/dir')
 
3342
        dest_wt.lock_read()
 
3343
        self.addCleanup(dest_wt.unlock)
 
3344
        # The r1-lib1 revision should NOT be merged into this one (this is a
 
3345
        # partial merge).
 
3346
        self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
 
3347
        self.assertTreeEntriesEqual(
 
3348
            [('', b'dest-root-id'),
 
3349
             ('dir', b'src-dir-id'),
 
3350
             ('dir/foo.c', b'src-foo.c-id'),
 
3351
             ], dest_wt)
 
3352
 
 
3353
    def test_only_file(self):
 
3354
        """An edge case: merge just one file, not a whole dir."""
 
3355
        dest_wt = self.setup_simple_branch('dest')
 
3356
        self.setup_simple_branch('two-file', ['file1.txt', 'file2.txt'])
 
3357
        self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
 
3358
        dest_wt.lock_read()
 
3359
        self.addCleanup(dest_wt.unlock)
 
3360
        # The r1-lib1 revision should NOT be merged into this one
 
3361
        self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
 
3362
        self.assertTreeEntriesEqual(
 
3363
            [('', b'dest-root-id'), ('file1.txt', b'two-file-file1.txt-id')],
 
3364
            dest_wt)
 
3365
 
 
3366
    def test_no_such_source_path(self):
 
3367
        """PathNotInTree is raised if the specified path in the source tree
 
3368
        does not exist.
 
3369
        """
 
3370
        dest_wt = self.setup_simple_branch('dest')
 
3371
        self.setup_simple_branch('src', ['dir/'])
 
3372
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
 
3373
                          'src/no-such-dir', 'dest/foo')
 
3374
        dest_wt.lock_read()
 
3375
        self.addCleanup(dest_wt.unlock)
 
3376
        # The dest tree is unmodified.
 
3377
        self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
 
3378
        self.assertTreeEntriesEqual([('', b'dest-root-id')], dest_wt)
 
3379
 
 
3380
    def test_no_such_target_path(self):
 
3381
        """PathNotInTree is also raised if the specified path in the target
 
3382
        tree does not exist.
 
3383
        """
 
3384
        dest_wt = self.setup_simple_branch('dest')
 
3385
        self.setup_simple_branch('src', ['file.txt'])
 
3386
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
 
3387
                          'src', 'dest/no-such-dir/foo')
 
3388
        dest_wt.lock_read()
 
3389
        self.addCleanup(dest_wt.unlock)
 
3390
        # The dest tree is unmodified.
 
3391
        self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
 
3392
        self.assertTreeEntriesEqual([('', b'dest-root-id')], dest_wt)
 
3393
 
 
3394
 
 
3395
class TestMergeHooks(TestCaseWithTransport):
 
3396
 
 
3397
    def setUp(self):
 
3398
        super(TestMergeHooks, self).setUp()
 
3399
        self.tree_a = self.make_branch_and_tree('tree_a')
 
3400
        self.build_tree_contents([('tree_a/file', b'content_1')])
 
3401
        self.tree_a.add('file', b'file-id')
 
3402
        self.tree_a.commit('added file')
 
3403
 
 
3404
        self.tree_b = self.tree_a.controldir.sprout(
 
3405
            'tree_b').open_workingtree()
 
3406
        self.build_tree_contents([('tree_b/file', b'content_2')])
 
3407
        self.tree_b.commit('modify file')
 
3408
 
 
3409
    def test_pre_merge_hook_inject_different_tree(self):
 
3410
        tree_c = self.tree_b.controldir.sprout('tree_c').open_workingtree()
 
3411
        self.build_tree_contents([('tree_c/file', b'content_3')])
 
3412
        tree_c.commit("more content")
 
3413
        calls = []
 
3414
 
 
3415
        def factory(merger):
 
3416
            self.assertIsInstance(merger, _mod_merge.Merge3Merger)
 
3417
            merger.other_tree = tree_c
 
3418
            calls.append(merger)
 
3419
        _mod_merge.Merger.hooks.install_named_hook('pre_merge',
 
3420
                                                   factory, 'test factory')
 
3421
        self.tree_a.merge_from_branch(self.tree_b.branch)
 
3422
 
 
3423
        self.assertFileEqual(b"content_3", 'tree_a/file')
 
3424
        self.assertLength(1, calls)
 
3425
 
 
3426
    def test_post_merge_hook_called(self):
 
3427
        calls = []
 
3428
 
 
3429
        def factory(merger):
 
3430
            self.assertIsInstance(merger, _mod_merge.Merge3Merger)
 
3431
            calls.append(merger)
 
3432
        _mod_merge.Merger.hooks.install_named_hook('post_merge',
 
3433
                                                   factory, 'test factory')
 
3434
 
 
3435
        self.tree_a.merge_from_branch(self.tree_b.branch)
 
3436
 
 
3437
        self.assertFileEqual(b"content_2", 'tree_a/file')
 
3438
        self.assertLength(1, calls)