~bzr-pqm/bzr/bzr.dev

4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2005-2010 Canonical Ltd
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
16
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
17
import os
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
18
import sys
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
19
1559.1.1 by Robert Collins
Merge in InterRepository API support.
20
import bzrlib
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
21
from bzrlib import (
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
22
    errors,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
23
    generate_ids,
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
24
    merge_directive,
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
25
    osutils,
26
    )
1534.10.20 by Aaron Bentley
Got all tests passing
27
from bzrlib.conflicts import ContentsConflict, TextConflict, PathConflict
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
28
from bzrlib.merge import (
29
    Merge3Merger,
30
    Diff3Merger,
31
    WeaveMerger,
32
    Merger,
33
    )
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
34
from bzrlib.osutils import getcwd, pathjoin
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
35
from bzrlib import progress
1534.7.140 by Aaron Bentley
Moved the merge stuff into merge.py
36
from bzrlib.transform import TreeTransform
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
37
from bzrlib.tests import TestCaseWithTransport, TestSkipped
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
38
from bzrlib.workingtree import WorkingTree
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
39
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
40
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
41
class MergeBuilder(object):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
42
    def __init__(self, dir=None):
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
43
        self.dir = osutils.mkdtemp(prefix="merge-test", dir=dir)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
44
        self.tree_root = generate_ids.gen_root_id()
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
45
        def wt(name):
46
           path = pathjoin(self.dir, name)
47
           os.mkdir(path)
1559.1.1 by Robert Collins
Merge in InterRepository API support.
48
           wt = bzrlib.bzrdir.BzrDir.create_standalone_workingtree(path)
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
49
           # the tests perform pulls, so need a branch that is writeable.
50
           wt.lock_write()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
51
           wt.set_root_id(self.tree_root)
2255.7.14 by John Arbash Meinel
Make set_root_id() a no-op if the id doesn't change,
52
           wt.flush()
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
53
           tt = TreeTransform(wt)
54
           return wt, tt
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
55
        self.base, self.base_tt = wt('base')
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
56
        self.this, self.this_tt = wt('this')
57
        self.other, self.other_tt = wt('other')
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
58
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
59
    def get_cset_path(self, parent, name):
60
        if name is None:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
61
            if parent is not None:
62
                raise AssertionError()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
63
            return None
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
64
        return pathjoin(self.cset.entries[parent].path, name)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
65
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
66
    def add_file(self, id, parent, name, contents, executable, this=True,
1558.7.12 by Aaron Bentley
Additional spurious conflict test
67
                 base=True, other=True):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
68
        def new_file(tt):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
69
            parent_id = tt.trans_id_file_id(parent)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
70
            tt.new_file(name, parent_id, contents, id, executable)
1558.7.12 by Aaron Bentley
Additional spurious conflict test
71
        for option, tt in self.selected_transforms(this, base, other):
72
            if option is True:
73
                new_file(tt)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
74
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
75
    def merge(self, merge_type=Merge3Merger, interesting_ids=None, **kwargs):
4797.21.1 by Aaron Bentley
Fix merge when this_tree is not a WorkingTree.
76
        merger = self.make_merger(merge_type, interesting_ids, **kwargs)
77
        merger.do_merge()
78
        return merger.cooked_conflicts
79
80
    def make_preview_transform(self):
81
        merger = self.make_merger(Merge3Merger, None, this_revision_tree=True)
82
        return merger.make_preview_transform()
83
84
    def make_merger(self, merge_type, interesting_ids,
85
            this_revision_tree=False, **kwargs):
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
86
        self.base_tt.apply()
87
        self.base.commit('base commit')
88
        for tt, wt in ((self.this_tt, self.this), (self.other_tt, self.other)):
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
89
            # why does this not do wt.pull() ?
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
90
            wt.branch.pull(self.base.branch)
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
91
            wt.set_parent_ids([wt.branch.last_revision()])
2255.7.14 by John Arbash Meinel
Make set_root_id() a no-op if the id doesn't change,
92
            wt.flush()
93
            # We maintain a write lock, so make sure changes are flushed to
94
            # disk first
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
95
            tt.apply()
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
96
            wt.commit('branch commit')
2255.7.14 by John Arbash Meinel
Make set_root_id() a no-op if the id doesn't change,
97
            wt.flush()
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
98
            if len(wt.branch.revision_history()) != 2:
99
                raise AssertionError()
1559.1.1 by Robert Collins
Merge in InterRepository API support.
100
        self.this.branch.fetch(self.other.branch)
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
101
        other_basis = self.other.branch.basis_tree()
4797.21.1 by Aaron Bentley
Fix merge when this_tree is not a WorkingTree.
102
        if this_revision_tree:
103
            self.this.commit('message')
104
            this_tree = self.this.basis_tree()
105
        else:
106
            this_tree = self.this
107
        merger = merge_type(this_tree, self.this, self.base, other_basis,
108
                            interesting_ids=interesting_ids, do_merge=False,
109
                            this_branch=self.this.branch, **kwargs)
110
        return merger
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
111
112
    def list_transforms(self):
113
        return [self.this_tt, self.base_tt, self.other_tt]
114
115
    def selected_transforms(self, this, base, other):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
116
        pairs = [(this, self.this_tt), (base, self.base_tt),
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
117
                 (other, self.other_tt)]
118
        return [(v, tt) for (v, tt) in pairs if v is not None]
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
119
1185.12.34 by Aaron Bentley
Added symlink three-way tests
120
    def add_symlink(self, id, parent, name, contents):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
121
        for tt in self.list_transforms():
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
122
            parent_id = tt.trans_id_file_id(parent)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
123
            tt.new_symlink(name, parent_id, contents, id)
1185.12.34 by Aaron Bentley
Added symlink three-way tests
124
1534.7.130 by Aaron Bentley
More conflict handling, test porting
125
    def remove_file(self, file_id, base=False, this=False, other=False):
126
        for option, tt in self.selected_transforms(this, base, other):
127
            if option is True:
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
128
                trans_id = tt.trans_id_file_id(file_id)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
129
                tt.cancel_creation(trans_id)
130
                tt.cancel_versioning(trans_id)
131
                tt.set_executability(None, trans_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
132
4797.9.2 by Vincent Ladeuil
More tests.
133
    def add_dir(self, file_id, parent, name, this=True, base=True, other=True):
134
        for option, tt in self.selected_transforms(this, base, other):
135
            if option is True:
136
                parent_id = tt.trans_id_file_id(parent)
137
                tt.new_directory(name, parent_id, file_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
138
139
    def change_name(self, id, base=None, this=None, other=None):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
140
        for val, tt in ((base, self.base_tt), (this, self.this_tt),
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
141
                        (other, self.other_tt)):
142
            if val is None:
143
                continue
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
144
            trans_id = tt.trans_id_file_id(id)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
145
            parent_id = tt.final_parent(trans_id)
146
            tt.adjust_path(val, parent_id, trans_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
147
1534.7.130 by Aaron Bentley
More conflict handling, test porting
148
    def change_parent(self, file_id, base=None, this=None, other=None):
149
        for parent, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
150
            trans_id  = tt.trans_id_file_id(file_id)
151
            parent_id = tt.trans_id_file_id(parent)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
152
            tt.adjust_path(tt.final_name(trans_id), parent_id, trans_id)
153
154
    def change_contents(self, file_id, base=None, this=None, other=None):
155
        for contents, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
156
            trans_id = tt.trans_id_file_id(file_id)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
157
            tt.cancel_creation(trans_id)
158
            tt.create_file(contents, trans_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
159
1185.12.34 by Aaron Bentley
Added symlink three-way tests
160
    def change_target(self, id, base=None, this=None, other=None):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
161
        for target, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
162
            trans_id = tt.trans_id_file_id(id)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
163
            tt.cancel_creation(trans_id)
164
            tt.create_symlink(target, trans_id)
1185.12.34 by Aaron Bentley
Added symlink three-way tests
165
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
166
    def change_perms(self, id, base=None, this=None, other=None):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
167
        for executability, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
168
            trans_id = tt.trans_id_file_id(id)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
169
            tt.set_executability(None, trans_id)
170
            tt.set_executability(executability, trans_id)
1185.12.34 by Aaron Bentley
Added symlink three-way tests
171
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
172
    def change_perms_tree(self, id, tree, mode):
173
        os.chmod(tree.full_path(id), mode)
174
175
    def apply_inv_change(self, inventory_change, orig_inventory):
176
        orig_inventory_by_path = {}
177
        for file_id, path in orig_inventory.iteritems():
178
            orig_inventory_by_path[path] = file_id
179
180
        def parent_id(file_id):
181
            try:
182
                parent_dir = os.path.dirname(orig_inventory[file_id])
183
            except:
184
                print file_id
185
                raise
186
            if parent_dir == "":
187
                return None
188
            return orig_inventory_by_path[parent_dir]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
189
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
190
        def new_path(file_id):
1963.2.1 by Robey Pointer
remove usage of has_key()
191
            if fild_id in inventory_change:
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
192
                return inventory_change[file_id]
193
            else:
194
                parent = parent_id(file_id)
195
                if parent is None:
196
                    return orig_inventory[file_id]
197
                dirname = new_path(parent)
1185.50.31 by John Arbash Meinel
[merge] Denys Duchier, more tests for test_merge_core, and fixup of one test.
198
                return pathjoin(dirname, os.path.basename(orig_inventory[file_id]))
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
199
200
        new_inventory = {}
201
        for file_id in orig_inventory.iterkeys():
202
            path = new_path(file_id)
203
            if path is None:
204
                continue
205
            new_inventory[file_id] = path
206
207
        for file_id, path in inventory_change.iteritems():
1963.2.1 by Robey Pointer
remove usage of has_key()
208
            if file_id in orig_inventory:
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
209
                continue
210
            new_inventory[file_id] = path
211
        return new_inventory
212
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
213
    def unlock(self):
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
214
        self.base.unlock()
215
        self.this.unlock()
216
        self.other.unlock()
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
217
218
    def cleanup(self):
219
        self.unlock()
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
220
        osutils.rmtree(self.dir)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
221
1448 by Robert Collins
revert symlinks correctly
222
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
223
class MergeTest(TestCaseWithTransport):
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
224
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
225
    def test_change_name(self):
226
        """Test renames"""
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
227
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
228
        builder.add_file("1", builder.tree_root, "name1", "hello1", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
229
        builder.change_name("1", other="name2")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
230
        builder.add_file("2", builder.tree_root, "name3", "hello2", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
231
        builder.change_name("2", base="name4")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
232
        builder.add_file("3", builder.tree_root, "name5", "hello3", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
233
        builder.change_name("3", this="name6")
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
234
        builder.merge()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
235
        builder.cleanup()
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
236
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
237
        builder.add_file("1", builder.tree_root, "name1", "hello1", False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
238
        builder.change_name("1", other="name2", this="name3")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
239
        conflicts = builder.merge()
1534.10.20 by Aaron Bentley
Got all tests passing
240
        self.assertEqual(conflicts, [PathConflict('name3', 'name2', '1')])
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
241
        builder.cleanup()
1558.2.2 by Aaron Bentley
Make remerge honour interesting-ids
242
243
    def test_merge_one(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
244
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
245
        builder.add_file("1", builder.tree_root, "name1", "hello1", True)
1558.2.2 by Aaron Bentley
Make remerge honour interesting-ids
246
        builder.change_contents("1", other="text4")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
247
        builder.add_file("2", builder.tree_root, "name2", "hello1", True)
1558.2.2 by Aaron Bentley
Make remerge honour interesting-ids
248
        builder.change_contents("2", other="text4")
249
        builder.merge(interesting_ids=["1"])
250
        self.assertEqual(builder.this.get_file("1").read(), "text4" )
251
        self.assertEqual(builder.this.get_file("2").read(), "hello1" )
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
252
        builder.cleanup()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
253
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
254
    def test_file_moves(self):
255
        """Test moves"""
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
256
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
257
        builder.add_dir("1", builder.tree_root, "dir1")
258
        builder.add_dir("2", builder.tree_root, "dir2")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
259
        builder.add_file("3", "1", "file1", "hello1", True)
260
        builder.add_file("4", "1", "file2", "hello2", True)
261
        builder.add_file("5", "1", "file3", "hello3", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
262
        builder.change_parent("3", other="2")
263
        builder.change_parent("4", this="2")
264
        builder.change_parent("5", base="2")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
265
        builder.merge()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
266
        builder.cleanup()
267
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
268
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
269
        builder.add_dir("1", builder.tree_root, "dir1")
270
        builder.add_dir("2", builder.tree_root, "dir2")
271
        builder.add_dir("3", builder.tree_root, "dir3")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
272
        builder.add_file("4", "1", "file1", "hello1", False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
273
        builder.change_parent("4", other="2", this="3")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
274
        conflicts = builder.merge()
1534.7.176 by abentley
Fixed up tests for Windows
275
        path2 = pathjoin('dir2', 'file1')
276
        path3 = pathjoin('dir3', 'file1')
1534.10.20 by Aaron Bentley
Got all tests passing
277
        self.assertEqual(conflicts, [PathConflict(path3, path2, '4')])
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
278
        builder.cleanup()
279
280
    def test_contents_merge(self):
281
        """Test merge3 merging"""
1534.7.130 by Aaron Bentley
More conflict handling, test porting
282
        self.do_contents_test(Merge3Merger)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
283
284
    def test_contents_merge2(self):
285
        """Test diff3 merging"""
2240.1.2 by Alexander Belchenko
test_contents_merge2: skip this test on win32
286
        if sys.platform == 'win32':
287
            raise TestSkipped("diff3 does not have --binary flag"
288
                              " and therefore always fails on win32")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
289
        try:
290
            self.do_contents_test(Diff3Merger)
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
291
        except errors.NoDiff3:
1534.7.130 by Aaron Bentley
More conflict handling, test porting
292
            raise TestSkipped("diff3 not available")
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
293
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
294
    def test_contents_merge3(self):
295
        """Test diff3 merging"""
296
        self.do_contents_test(WeaveMerger)
297
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
298
    def test_reprocess_weave(self):
299
        # Reprocess works on weaves, and behaves as expected
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
300
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
301
        builder.add_file('a', builder.tree_root, 'blah', 'a', False)
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
302
        builder.change_contents('a', this='b\nc\nd\ne\n', other='z\nc\nd\ny\n')
303
        builder.merge(WeaveMerger, reprocess=True)
304
        expected = """<<<<<<< TREE
305
b
306
=======
307
z
308
>>>>>>> MERGE-SOURCE
309
c
310
d
311
<<<<<<< TREE
312
e
313
=======
314
y
315
>>>>>>> MERGE-SOURCE
316
"""
317
        self.assertEqualDiff(builder.this.get_file("a").read(), expected)
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
318
        builder.cleanup()
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
319
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
320
    def do_contents_test(self, merge_factory):
321
        """Test merging with specified ContentsChange factory"""
322
        builder = self.contents_test_success(merge_factory)
323
        builder.cleanup()
324
        self.contents_test_conflicts(merge_factory)
325
326
    def contents_test_success(self, merge_factory):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
327
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
328
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
329
        builder.change_contents("1", other="text4")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
330
        builder.add_file("2", builder.tree_root, "name3", "text2", False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
331
        builder.change_contents("2", base="text5")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
332
        builder.add_file("3", builder.tree_root, "name5", "text3", True)
333
        builder.add_file("4", builder.tree_root, "name6", "text4", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
334
        builder.remove_file("4", base=True)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
335
        builder.add_file("5", builder.tree_root, "name7", "a\nb\nc\nd\ne\nf\n",
336
                         True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
337
        builder.change_contents("5", other="a\nz\nc\nd\ne\nf\n",
1558.6.4 by Aaron Bentley
Fixed merge-type weave
338
                                     this="a\nb\nc\nd\ne\nz\n")
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
339
        conflicts = builder.merge(merge_factory)
340
        try:
341
            self.assertEqual([], conflicts)
1551.8.40 by Aaron Bentley
Update test case style
342
            self.assertEqual("text4", builder.this.get_file("1").read())
343
            self.assertEqual("text2", builder.this.get_file("2").read())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
344
            self.assertEqual("a\nz\nc\nd\ne\nz\n",
1551.8.40 by Aaron Bentley
Update test case style
345
                             builder.this.get_file("5").read())
346
            self.assertTrue(builder.this.is_executable("1"))
347
            self.assertFalse(builder.this.is_executable("2"))
348
            self.assertTrue(builder.this.is_executable("3"))
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
349
        except:
350
            builder.unlock()
351
            raise
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
352
        return builder
353
354
    def contents_test_conflicts(self, merge_factory):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
355
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
356
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
357
        builder.change_contents("1", other="text4", this="text3")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
358
        builder.add_file("2", builder.tree_root, "name2", "text1", True)
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
359
        builder.change_contents("2", other="\x00", this="text3")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
360
        builder.add_file("3", builder.tree_root, "name3", "text5", False)
1534.10.35 by Aaron Bentley
Merge handles contents + executable + deletion conflict
361
        builder.change_perms("3", this=True)
362
        builder.change_contents('3', this='moretext')
363
        builder.remove_file('3', other=True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
364
        conflicts = builder.merge(merge_factory)
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
365
        self.assertEqual(conflicts, [TextConflict('name1', file_id='1'),
1534.10.35 by Aaron Bentley
Merge handles contents + executable + deletion conflict
366
                                     ContentsConflict('name2', file_id='2'),
367
                                     ContentsConflict('name3', file_id='3')])
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
368
        self.assertEqual(builder.this.get_file('2').read(), '\x00')
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
369
        builder.cleanup()
370
1185.12.34 by Aaron Bentley
Added symlink three-way tests
371
    def test_symlink_conflicts(self):
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
372
        if sys.platform != "win32":
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
373
            builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
374
            builder.add_symlink("2", builder.tree_root, "name2", "target1")
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
375
            builder.change_target("2", other="target4", base="text3")
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
376
            conflicts = builder.merge()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
377
            self.assertEqual(conflicts, [ContentsConflict('name2',
1534.10.20 by Aaron Bentley
Got all tests passing
378
                                                          file_id='2')])
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
379
            builder.cleanup()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
380
1185.12.34 by Aaron Bentley
Added symlink three-way tests
381
    def test_symlink_merge(self):
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
382
        if sys.platform != "win32":
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
383
            builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
384
            builder.add_symlink("1", builder.tree_root, "name1", "target1")
385
            builder.add_symlink("2", builder.tree_root, "name2", "target1")
386
            builder.add_symlink("3", builder.tree_root, "name3", "target1")
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
387
            builder.change_target("1", this="target2")
388
            builder.change_target("2", base="target2")
389
            builder.change_target("3", other="target2")
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
390
            builder.merge()
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
391
            self.assertEqual(builder.this.get_symlink_target("1"), "target2")
392
            self.assertEqual(builder.this.get_symlink_target("2"), "target1")
393
            self.assertEqual(builder.this.get_symlink_target("3"), "target2")
394
            builder.cleanup()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
395
1534.7.143 by Aaron Bentley
Prevented get_trans_id from automatically versioning file ids
396
    def test_no_passive_add(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
397
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
398
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1534.7.143 by Aaron Bentley
Prevented get_trans_id from automatically versioning file ids
399
        builder.remove_file("1", this=True)
400
        builder.merge()
1534.7.146 by Aaron Bentley
Fixed merge so tree root is auto-preserved, not by conflict resolution
401
        builder.cleanup()
1534.7.143 by Aaron Bentley
Prevented get_trans_id from automatically versioning file ids
402
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
403
    def test_perms_merge(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
404
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
405
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
406
        builder.change_perms("1", other=False)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
407
        builder.add_file("2", builder.tree_root, "name2", "text2", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
408
        builder.change_perms("2", base=False)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
409
        builder.add_file("3", builder.tree_root, "name3", "text3", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
410
        builder.change_perms("3", this=False)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
411
        builder.add_file('4', builder.tree_root, 'name4', 'text4', False)
1534.7.142 by Aaron Bentley
Fixed executability conflicts
412
        builder.change_perms('4', this=True)
413
        builder.remove_file('4', base=True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
414
        builder.merge()
415
        self.assertIs(builder.this.is_executable("1"), False)
416
        self.assertIs(builder.this.is_executable("2"), True)
417
        self.assertIs(builder.this.is_executable("3"), False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
418
        builder.cleanup();
1092.1.25 by Robert Collins
prepare to write merge tests
419
1185.50.50 by John Arbash Meinel
[patch] Aaron Bentley: Merge deletes foo.new
420
    def test_new_suffix(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
421
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
422
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
423
        builder.change_contents("1", other="text3")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
424
        builder.add_file("2", builder.tree_root, "name1.new", "text2", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
425
        builder.merge()
426
        os.lstat(builder.this.id2abspath("2"))
427
        builder.cleanup()
1185.50.50 by John Arbash Meinel
[patch] Aaron Bentley: Merge deletes foo.new
428
1558.7.12 by Aaron Bentley
Additional spurious conflict test
429
    def test_spurious_conflict(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
430
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
431
        builder.add_file("1", builder.tree_root, "name1", "text1", False)
1558.7.12 by Aaron Bentley
Additional spurious conflict test
432
        builder.remove_file("1", other=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
433
        builder.add_file("2", builder.tree_root, "name1", "text1", False,
1731.1.33 by Aaron Bentley
Revert no-special-root changes
434
                         this=False, base=False)
1558.7.12 by Aaron Bentley
Additional spurious conflict test
435
        conflicts = builder.merge()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
436
        self.assertEqual(conflicts, [])
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
437
        builder.cleanup()
1558.7.12 by Aaron Bentley
Additional spurious conflict test
438
2590.2.5 by Aaron Bentley
Allow selected files to be specified instead of selected ids
439
    def test_merge_one_renamed(self):
440
        builder = MergeBuilder(getcwd())
441
        builder.add_file('1', builder.tree_root, 'name1', 'text1a', False)
442
        builder.change_name('1', this='name2')
443
        builder.change_contents('1', other='text2')
444
        builder.merge(interesting_files=['name2'])
445
        self.assertEqual('text2', builder.this.get_file('1').read())
446
        builder.cleanup()
1448 by Robert Collins
revert symlinks correctly
447
4869.3.1 by Andrew Bennetts
Basic per-file merge hook.
448
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
449
class FunctionalMergeTest(TestCaseWithTransport):
1092.1.25 by Robert Collins
prepare to write merge tests
450
451
    def test_trivial_star_merge(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
452
        """Test that merges in a star shape Just Work."""
1092.1.33 by Robert Collins
pull the important stuff out of cmd_branch.run to branch.copy_branch
453
        # John starts a branch
1092.1.26 by Robert Collins
start writing star-topology test, realise we need smart-add change
454
        self.build_tree(("original/", "original/file1", "original/file2"))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
455
        tree = self.make_branch_and_tree('original')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
456
        branch = tree.branch
2568.2.7 by Robert Collins
Fix missed tests.
457
        tree.smart_add(["original"])
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
458
        tree.commit("start branch.", verbose=False)
1092.1.33 by Robert Collins
pull the important stuff out of cmd_branch.run to branch.copy_branch
459
        # Mary branches it.
1092.1.34 by Robert Collins
unbreak cmd_branch now that something tests the core of it..
460
        self.build_tree(("mary/",))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
461
        branch.bzrdir.clone("mary")
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
462
        # Now John commits a change
463
        file = open("original/file1", "wt")
464
        file.write("John\n")
465
        file.close()
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
466
        tree.commit("change file1")
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
467
        # Mary does too
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
468
        mary_tree = WorkingTree.open('mary')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
469
        mary_branch = mary_tree.branch
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
470
        file = open("mary/file2", "wt")
471
        file.write("Mary\n")
472
        file.close()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
473
        mary_tree.commit("change file2")
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
474
        # john should be able to merge with no conflicts.
1092.1.41 by Robert Collins
merge from abently, take his fixes for merge in preference
475
        base = [None, None]
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
476
        other = ("mary", -1)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
477
        tree.merge_from_branch(mary_tree.branch)
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
478
        self.assertEqual("John\n", open("original/file1", "rt").read())
479
        self.assertEqual("Mary\n", open("original/file2", "rt").read())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
480
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
481
    def test_conflicts(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
482
        wta = self.make_branch_and_tree('a')
4634.101.12 by John Arbash Meinel
Clean up the test_conflicts file for newer apis.
483
        self.build_tree_contents([('a/file', 'contents\n')])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
484
        wta.add('file')
485
        wta.commit('base revision', allow_pointless=False)
4634.101.12 by John Arbash Meinel
Clean up the test_conflicts file for newer apis.
486
        d_b = wta.branch.bzrdir.clone('b')
487
        self.build_tree_contents([('a/file', 'other contents\n')])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
488
        wta.commit('other revision', allow_pointless=False)
4634.101.12 by John Arbash Meinel
Clean up the test_conflicts file for newer apis.
489
        self.build_tree_contents([('b/file', 'this contents contents\n')])
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
490
        wtb = d_b.open_workingtree()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
491
        wtb.commit('this revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
492
        self.assertEqual(1, wtb.merge_from_branch(wta.branch))
4634.101.11 by John Arbash Meinel
Actually there was a semi-whitebox test, now updated.
493
        self.failUnlessExists('b/file.THIS')
494
        self.failUnlessExists('b/file.BASE')
495
        self.failUnlessExists('b/file.OTHER')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
496
        wtb.revert()
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
497
        self.assertEqual(1, wtb.merge_from_branch(wta.branch,
498
                                                  merge_type=WeaveMerger))
4634.101.11 by John Arbash Meinel
Actually there was a semi-whitebox test, now updated.
499
        self.failUnlessExists('b/file')
500
        self.failUnlessExists('b/file.THIS')
501
        self.failUnlessExists('b/file.BASE')
502
        self.failUnlessExists('b/file.OTHER')
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
503
4634.101.14 by John Arbash Meinel
Add a knownFailure case where we could drop a .BASE but fail to.
504
    def test_weave_conflicts_not_in_base(self):
505
        builder = self.make_branch_builder('source')
506
        builder.start_series()
4634.101.15 by John Arbash Meinel
Add bug #494197 as the source of the KnownFailure test.
507
        # See bug #494197
4634.101.14 by John Arbash Meinel
Add a knownFailure case where we could drop a .BASE but fail to.
508
        #  A        base revision (before criss-cross)
509
        #  |\
510
        #  B C      B does nothing, C adds 'foo'
511
        #  |X|
512
        #  D E      D and E modify foo in incompatible ways
513
        #
514
        # Merging will conflict, with C as a clean base text. However, the
515
        # current code uses A as the global base and 'foo' doesn't exist there.
516
        # It isn't trivial to create foo.BASE because it tries to look up
517
        # attributes like 'executable' in A.
518
        builder.build_snapshot('A-id', None, [
519
            ('add', ('', 'TREE_ROOT', 'directory', None))])
520
        builder.build_snapshot('B-id', ['A-id'], [])
521
        builder.build_snapshot('C-id', ['A-id'], [
522
            ('add', ('foo', 'foo-id', 'file', 'orig\ncontents\n'))])
523
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [
524
            ('add', ('foo', 'foo-id', 'file', 'orig\ncontents\nand D\n'))])
525
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [
526
            ('modify', ('foo-id', 'orig\ncontents\nand E\n'))])
527
        builder.finish_series()
528
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
529
        self.assertEqual(1, tree.merge_from_branch(tree.branch,
530
                                                   to_revision='D-id',
531
                                                   merge_type=WeaveMerger))
532
        self.failUnlessExists('tree/foo.THIS')
533
        self.failUnlessExists('tree/foo.OTHER')
534
        self.expectFailure('fail to create .BASE in some criss-cross merges',
535
            self.failUnlessExists, 'tree/foo.BASE')
536
        self.failUnlessExists('tree/foo.BASE')
537
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
538
    def test_merge_unrelated(self):
539
        """Sucessfully merges unrelated branches with no common names"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
540
        wta = self.make_branch_and_tree('a')
541
        a = wta.branch
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
542
        file('a/a_file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
543
        wta.add('a_file')
544
        wta.commit('a_revision', allow_pointless=False)
545
        wtb = self.make_branch_and_tree('b')
546
        b = wtb.branch
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
547
        file('b/b_file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
548
        wtb.add('b_file')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
549
        b_rev = wtb.commit('b_revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
550
        wta.merge_from_branch(wtb.branch, b_rev, 'null:')
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
551
        self.assert_(os.path.lexists('a/b_file'))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
552
        self.assertEqual([b_rev], wta.get_parent_ids()[1:])
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
553
554
    def test_merge_unrelated_conflicting(self):
555
        """Sucessfully merges unrelated branches with common names"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
556
        wta = self.make_branch_and_tree('a')
557
        a = wta.branch
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
558
        file('a/file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
559
        wta.add('file')
560
        wta.commit('a_revision', allow_pointless=False)
561
        wtb = self.make_branch_and_tree('b')
562
        b = wtb.branch
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
563
        file('b/file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
564
        wtb.add('file')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
565
        b_rev = wtb.commit('b_revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
566
        wta.merge_from_branch(wtb.branch, b_rev, 'null:')
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
567
        self.assert_(os.path.lexists('a/file'))
568
        self.assert_(os.path.lexists('a/file.moved'))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
569
        self.assertEqual([b_rev], wta.get_parent_ids()[1:])
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
570
571
    def test_merge_deleted_conflicts(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
572
        wta = self.make_branch_and_tree('a')
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
573
        file('a/file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
574
        wta.add('file')
575
        wta.commit('a_revision', allow_pointless=False)
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
576
        self.run_bzr('branch a b')
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
577
        os.remove('a/file')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
578
        wta.commit('removed file', allow_pointless=False)
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
579
        file('b/file', 'wb').write('changed contents\n')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
580
        wtb = WorkingTree.open('b')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
581
        wtb.commit('changed file', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
582
        wtb.merge_from_branch(wta.branch, wta.branch.last_revision(),
583
                              wta.branch.get_rev_id(1))
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
584
        self.failIf(os.path.lexists('b/file'))
585
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
586
    def test_merge_metadata_vs_deletion(self):
587
        """Conflict deletion vs metadata change"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
588
        a_wt = self.make_branch_and_tree('a')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
589
        file('a/file', 'wb').write('contents\n')
1508.1.15 by Robert Collins
Merge from mpool.
590
        a_wt.add('file')
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
591
        a_wt.commit('r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
592
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
593
        b_wt = WorkingTree.open('b')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
594
        os.chmod('b/file', 0755)
595
        os.remove('a/file')
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
596
        a_wt.commit('removed a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
597
        self.assertEqual(a_wt.branch.revno(), 2)
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
598
        self.assertFalse(os.path.exists('a/file'))
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
599
        b_wt.commit('exec a')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
600
        a_wt.merge_from_branch(b_wt.branch, b_wt.last_revision(), 'null:')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
601
        self.assert_(os.path.exists('a/file'))
1185.31.14 by John Arbash Meinel
[merge] bzr.dev
602
1185.33.102 by Denys Duchier
two new tests suggested by abentley
603
    def test_merge_swapping_renames(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
604
        a_wt = self.make_branch_and_tree('a')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
605
        file('a/un','wb').write('UN')
606
        file('a/deux','wb').write('DEUX')
2323.6.14 by Martin Pool
clearer test data for test merge_swapping_rename
607
        a_wt.add('un', 'un-id')
608
        a_wt.add('deux', 'deux-id')
1508.1.24 by Robert Collins
Add update command for use with checkouts.
609
        a_wt.commit('r0', rev_id='r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
610
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
611
        b_wt = WorkingTree.open('b')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
612
        b_wt.rename_one('un','tmp')
613
        b_wt.rename_one('deux','un')
614
        b_wt.rename_one('tmp','deux')
1508.1.24 by Robert Collins
Add update command for use with checkouts.
615
        b_wt.commit('r1', rev_id='r1')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
616
        self.assertEqual(0, a_wt.merge_from_branch(b_wt.branch,
617
            b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1)))
1508.1.24 by Robert Collins
Add update command for use with checkouts.
618
        self.failUnlessExists('a/un')
619
        self.failUnless('a/deux')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
620
        self.assertFalse(os.path.exists('a/tmp'))
621
        self.assertEqual(file('a/un').read(),'DEUX')
622
        self.assertEqual(file('a/deux').read(),'UN')
623
624
    def test_merge_delete_and_add_same(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
625
        a_wt = self.make_branch_and_tree('a')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
626
        file('a/file', 'wb').write('THIS')
627
        a_wt.add('file')
628
        a_wt.commit('r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
629
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
630
        b_wt = WorkingTree.open('b')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
631
        os.remove('b/file')
632
        b_wt.commit('r1')
633
        file('b/file', 'wb').write('THAT')
634
        b_wt.add('file')
635
        b_wt.commit('r2')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
636
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
637
                               b_wt.branch.get_rev_id(1))
1185.33.102 by Denys Duchier
two new tests suggested by abentley
638
        self.assert_(os.path.exists('a/file'))
639
        self.assertEqual(file('a/file').read(),'THAT')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
640
641
    def test_merge_rename_before_create(self):
642
        """rename before create
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
643
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
644
        This case requires that you must not do creates
645
        before move-into-place:
646
647
        $ touch foo
648
        $ bzr add foo
649
        $ bzr commit
650
        $ bzr mv foo bar
651
        $ touch foo
652
        $ bzr add foo
653
        $ bzr commit
654
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
655
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
656
        file('a/foo', 'wb').write('A/FOO')
657
        a_wt.add('foo')
658
        a_wt.commit('added foo')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
659
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
660
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
661
        b_wt.rename_one('foo', 'bar')
662
        file('b/foo', 'wb').write('B/FOO')
663
        b_wt.add('foo')
664
        b_wt.commit('moved foo to bar, added new foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
665
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
666
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
667
668
    def test_merge_create_before_rename(self):
669
        """create before rename, target parents before children
670
671
        This case requires that you must not do move-into-place
672
        before creates, and that you must not do children after
673
        parents:
674
675
        $ touch foo
676
        $ bzr add foo
677
        $ bzr commit
678
        $ bzr mkdir bar
679
        $ bzr add bar
680
        $ bzr mv foo bar/foo
681
        $ bzr commit
682
        """
683
        os.mkdir('a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
684
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
685
        file('a/foo', 'wb').write('A/FOO')
686
        a_wt.add('foo')
687
        a_wt.commit('added foo')
2530.3.2 by Martin Pool
Refactoring run_bzr code into more of a common base.
688
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
689
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
690
        os.mkdir('b/bar')
691
        b_wt.add('bar')
692
        b_wt.rename_one('foo', 'bar/foo')
693
        b_wt.commit('created bar dir, moved foo into bar')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
694
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
695
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
696
697
    def test_merge_rename_to_temp_before_delete(self):
698
        """rename to temp before delete, source children before parents
699
700
        This case requires that you must not do deletes before
701
        move-out-of-the-way, and that you must not do children
702
        after parents:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
703
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
704
        $ mkdir foo
705
        $ touch foo/bar
706
        $ bzr add foo/bar
707
        $ bzr commit
708
        $ bzr mv foo/bar bar
709
        $ rmdir foo
710
        $ bzr commit
711
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
712
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
713
        os.mkdir('a/foo')
714
        file('a/foo/bar', 'wb').write('A/FOO/BAR')
715
        a_wt.add('foo')
716
        a_wt.add('foo/bar')
717
        a_wt.commit('added foo/bar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
718
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
719
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
720
        b_wt.rename_one('foo/bar', 'bar')
721
        os.rmdir('b/foo')
722
        b_wt.remove('foo')
723
        b_wt.commit('moved foo/bar to bar, deleted foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
724
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
725
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
726
727
    def test_merge_delete_before_rename_to_temp(self):
728
        """delete before rename to temp
729
730
        This case requires that you must not do
731
        move-out-of-the-way before deletes:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
732
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
733
        $ touch foo
734
        $ touch bar
735
        $ bzr add foo bar
736
        $ bzr commit
737
        $ rm foo
738
        $ bzr rm foo
739
        $ bzr mv bar foo
740
        $ bzr commit
741
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
742
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
743
        file('a/foo', 'wb').write('A/FOO')
744
        file('a/bar', 'wb').write('A/BAR')
745
        a_wt.add('foo')
746
        a_wt.add('bar')
747
        a_wt.commit('added foo and bar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
748
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
749
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
750
        os.unlink('b/foo')
751
        b_wt.remove('foo')
752
        b_wt.rename_one('bar', 'foo')
753
        b_wt.commit('deleted foo, renamed bar to foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
754
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
755
                               b_wt.branch.get_rev_id(1))
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
756
757
758
class TestMerger(TestCaseWithTransport):
759
760
    def set_up_trees(self):
761
        this = self.make_branch_and_tree('this')
762
        this.commit('rev1', rev_id='rev1')
763
        other = this.bzrdir.sprout('other').open_workingtree()
764
        this.commit('rev2a', rev_id='rev2a')
765
        other.commit('rev2b', rev_id='rev2b')
766
        return this, other
767
768
    def test_from_revision_ids(self):
769
        this, other = self.set_up_trees()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
770
        self.assertRaises(errors.NoSuchRevision, Merger.from_revision_ids,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
771
                          None, this, 'rev2b')
3010.1.8 by Robert Collins
test_merge_core locking correctness.
772
        this.lock_write()
773
        self.addCleanup(this.unlock)
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
774
        merger = Merger.from_revision_ids(None, this,
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
775
            'rev2b', other_branch=other.branch)
776
        self.assertEqual('rev2b', merger.other_rev_id)
777
        self.assertEqual('rev1', merger.base_rev_id)
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
778
        merger = Merger.from_revision_ids(None, this,
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
779
            'rev2b', 'rev2a', other_branch=other.branch)
780
        self.assertEqual('rev2a', merger.base_rev_id)
781
782
    def test_from_uncommitted(self):
783
        this, other = self.set_up_trees()
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
784
        merger = Merger.from_uncommitted(this, other, None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
785
        self.assertIs(other, merger.other_tree)
786
        self.assertIs(None, merger.other_rev_id)
787
        self.assertEqual('rev2b', merger.base_rev_id)
788
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
789
    def prepare_for_merging(self):
790
        this, other = self.set_up_trees()
791
        other.commit('rev3', rev_id='rev3')
792
        this.lock_write()
793
        self.addCleanup(this.unlock)
794
        return this, other
795
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
796
    def test_from_mergeable(self):
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
797
        this, other = self.prepare_for_merging()
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
798
        md = merge_directive.MergeDirective2.from_objects(
799
            other.branch.repository, 'rev3', 0, 0, 'this')
3010.1.8 by Robert Collins
test_merge_core locking correctness.
800
        other.lock_read()
801
        self.addCleanup(other.unlock)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
802
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
803
            None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
804
        md.patch = None
805
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
806
            None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
807
        self.assertEqual('inapplicable', verified)
808
        self.assertEqual('rev3', merger.other_rev_id)
809
        self.assertEqual('rev1', merger.base_rev_id)
810
        md.base_revision_id = 'rev2b'
811
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
812
            None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
813
        self.assertEqual('rev2b', merger.base_rev_id)
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
814
815
    def test_from_mergeable_old_merge_directive(self):
816
        this, other = self.prepare_for_merging()
817
        other.lock_write()
818
        self.addCleanup(other.unlock)
819
        md = merge_directive.MergeDirective.from_objects(
820
            other.branch.repository, 'rev3', 0, 0, 'this')
821
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
822
            None)
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
823
        self.assertEqual('rev3', merger.other_rev_id)
824
        self.assertEqual('rev1', merger.base_rev_id)