/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4634.149.1 by Vincent Ladeuil
Fix imports.
1
# Copyright (C) 2006-2010 Canonical Ltd
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
17
18
"""Tests for the WorkingTree.merge_from_branch api."""
19
1551.10.31 by Aaron Bentley
Fix WorkingTree4._iter_changes with pending merges and deleted files
20
import os
21
1551.15.69 by Aaron Bentley
Add merge_type to merge_from_branch
22
from bzrlib import (
23
    errors,
24
    merge
25
    )
4634.149.1 by Vincent Ladeuil
Fix imports.
26
from bzrlib.tests import per_workingtree
27
28
29
class TestMergeFromBranch(per_workingtree.TestCaseWithWorkingTree):
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
30
31
    def create_two_trees_for_merging(self):
32
        """Create two trees that can be merged from.
33
34
        This sets self.tree_from, self.first_rev, self.tree_to, self.second_rev
35
        and self.to_second_rev.
36
        """
37
        self.tree_from = self.make_branch_and_tree('from')
38
        self.first_rev = self.tree_from.commit('first post')
39
        self.tree_to = self.tree_from.bzrdir.sprout('to').open_workingtree()
40
        self.second_rev = self.tree_from.commit('second rev', allow_pointless=True)
41
        self.to_second_rev = self.tree_to.commit('second rev', allow_pointless=True)
42
43
    def test_smoking_merge(self):
44
        """Smoke test of merge_from_branch."""
45
        self.create_two_trees_for_merging()
46
        self.tree_to.merge_from_branch(self.tree_from.branch)
47
        self.assertEqual([self.to_second_rev, self.second_rev],
48
            self.tree_to.get_parent_ids())
49
50
    def test_merge_to_revision(self):
51
        """Merge from a branch to a revision that is not the tip."""
52
        self.create_two_trees_for_merging()
53
        self.third_rev = self.tree_from.commit('real_tip')
54
        self.tree_to.merge_from_branch(self.tree_from.branch,
55
            to_revision=self.second_rev)
56
        self.assertEqual([self.to_second_rev, self.second_rev],
57
            self.tree_to.get_parent_ids())
1551.10.31 by Aaron Bentley
Fix WorkingTree4._iter_changes with pending merges and deleted files
58
59
    def test_compare_after_merge(self):
60
        tree_a = self.make_branch_and_tree('tree_a')
61
        self.build_tree_contents([('tree_a/file', 'text-a')])
62
        tree_a.add('file')
63
        tree_a.commit('added file')
64
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
65
        os.unlink('tree_a/file')
66
        tree_a.commit('deleted file')
67
        self.build_tree_contents([('tree_b/file', 'text-b')])
68
        tree_b.commit('changed file')
69
        tree_a.merge_from_branch(tree_b.branch)
70
        tree_a.lock_read()
71
        self.addCleanup(tree_a.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
72
        list(tree_a.iter_changes(tree_a.basis_tree()))
2490.2.28 by Aaron Bentley
Fix handling of null revision
73
74
    def test_merge_empty(self):
75
        tree_a = self.make_branch_and_tree('tree_a')
76
        self.build_tree_contents([('tree_a/file', 'text-a')])
77
        tree_a.add('file')
78
        tree_a.commit('added file')
79
        tree_b = self.make_branch_and_tree('treeb')
80
        self.assertRaises(errors.NoCommits, tree_a.merge_from_branch,
81
                          tree_b.branch)
82
        tree_b.merge_from_branch(tree_a.branch)
1551.15.68 by Aaron Bentley
Add support for base to merge_from_branch
83
84
    def test_merge_base(self):
85
        tree_a = self.make_branch_and_tree('tree_a')
86
        self.build_tree_contents([('tree_a/file', 'text-a')])
87
        tree_a.add('file')
88
        tree_a.commit('added file', rev_id='rev_1')
89
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
90
        os.unlink('tree_a/file')
91
        tree_a.commit('deleted file')
92
        self.build_tree_contents([('tree_b/file', 'text-b')])
93
        tree_b.commit('changed file')
94
        self.assertRaises(errors.PointlessMerge, tree_a.merge_from_branch,
95
            tree_b.branch, from_revision=tree_b.branch.last_revision())
96
        tree_a.merge_from_branch(tree_b.branch, from_revision='rev_1')
97
        tree_a.lock_read()
98
        self.addCleanup(tree_a.unlock)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
99
        changes = list(tree_a.iter_changes(tree_a.basis_tree()))
1551.15.68 by Aaron Bentley
Add support for base to merge_from_branch
100
        self.assertEqual(1, len(changes))
1551.15.69 by Aaron Bentley
Add merge_type to merge_from_branch
101
102
    def test_merge_type(self):
103
        this = self.make_branch_and_tree('this')
104
        self.build_tree_contents([('this/foo', 'foo')])
105
        this.add('foo', 'foo-id')
106
        this.commit('added foo')
107
        other = this.bzrdir.sprout('other').open_workingtree()
108
        self.build_tree_contents([('other/foo', 'bar')])
109
        other.commit('content -> bar')
110
        self.build_tree_contents([('this/foo', 'baz')])
111
        this.commit('content -> baz')
112
        class QuxMerge(merge.Merge3Merger):
113
            def text_merge(self, file_id, trans_id):
114
                self.tt.create_file('qux', trans_id)
115
        this.merge_from_branch(other.branch, merge_type=QuxMerge)
116
        self.assertEqual('qux', this.get_file_text('foo-id'))
4634.149.2 by Vincent Ladeuil
Reproduce bug #373898.
117
118
119
class TestMergedBranch(per_workingtree.TestCaseWithWorkingTree):
120
121
    def make_inner_branch(self):
122
        bld_inner = self.make_branch_builder('inner')
123
        bld_inner.start_series()
124
        bld_inner.build_snapshot(
125
            '1', None,
126
            [('add', ('', 'inner-root-id', 'directory', '')),
127
             ('add', ('dir', 'dir-id', 'directory', '')),
128
             ('add', ('dir/file1', 'file1-id', 'file', 'file1 content\n')),
129
             ('add', ('file3', 'file3-id', 'file', 'file3 content\n')),
130
             ])
131
        bld_inner.build_snapshot(
132
            '3', ['1'], [('modify', ('file3-id', 'new file3 contents\n')),])
133
        bld_inner.build_snapshot(
134
            '2', ['1'],
135
            [('add', ('dir/file2', 'file2-id', 'file', 'file2 content\n')),
136
             ])
137
        bld_inner.finish_series()
138
        br = bld_inner.get_branch()
139
        return br
140
141
    def assertTreeLayout(self, expected, tree):
142
        tree.lock_read()
143
        try:
144
            actual = [e[0] for e in tree.list_files()]
145
            # list_files doesn't guarantee order
146
            actual = sorted(actual)
147
            self.assertEqual(expected, actual)
148
        finally:
149
            tree.unlock()
150
151
    def make_outer_tree(self):
152
        outer = self.make_branch_and_tree('outer')
153
        self.build_tree_contents([('outer/foo', 'foo')])
154
        outer.add('foo', 'foo-id')
155
        outer.commit('added foo')
156
        inner = self.make_inner_branch()
157
        outer.merge_from_branch(inner, to_revision='1', from_revision='null:')
158
        outer.commit('merge inner branch')
159
        outer.mkdir('dir-outer', 'dir-outer-id')
160
        outer.move(['dir', 'file3'], to_dir='dir-outer')
161
        outer.commit('rename imported dir and file3 to dir-outer')
162
        return outer, inner
163
164
    def test_file1_deleted_in_dir(self):
165
        outer, inner = self.make_outer_tree()
166
        outer.remove(['dir-outer/dir/file1'], keep_files=False)
167
        outer.commit('delete file1')
168
        outer.merge_from_branch(inner)
169
        outer.commit('merge the rest')
170
        self.assertTreeLayout(['dir-outer',
171
                               'dir-outer/dir',
172
                               'dir-outer/dir/file2',
173
                               'dir-outer/file3',
174
                               'foo'],
175
                              outer)
176
177
    def test_file3_deleted_in_root(self):
178
        # Reproduce bug #375898
179
        outer, inner = self.make_outer_tree()
180
        outer.remove(['dir-outer/file3'], keep_files=False)
181
        outer.commit('delete file3')
182
        outer.merge_from_branch(inner)
183
        outer.commit('merge the rest')
184
        self.assertTreeLayout(['dir-outer',
185
                               'dir-outer/dir',
186
                               'dir-outer/dir/file1',
187
                               'dir-outer/dir/file2',
188
                               'foo'],
189
                              outer)
190
191
192
    def test_file3_in_root_conflicted(self):
193
        outer, inner = self.make_outer_tree()
194
        outer.remove(['dir-outer/file3'], keep_files=False)
195
        outer.commit('delete file3')
196
        nb_conflicts = outer.merge_from_branch(inner, to_revision='3')
197
        self.assertEqual(4, nb_conflicts)
198
        self.assertTreeLayout(['dir-outer',
199
                               'dir-outer/dir',
200
                               'dir-outer/dir/file1',
201
                               # Ideally th conflict helpers should be in
202
                               # dir-outer/dir but since we can't easily find
203
                               # back the file3 -> outer-dir/dir rename, root
204
                               # is good enough -- vila 20100401
205
                               'file3.BASE',
206
                               'file3.OTHER',
207
                               'foo'],
208
                              outer)
209