~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1685.1.80 by Wouter van Heyst
more code cleanup
2
#
1185.54.23 by Aaron Bentley
Added unit tests for find_unmerged
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.
1685.1.80 by Wouter van Heyst
more code cleanup
7
#
1185.54.23 by Aaron Bentley
Added unit tests for find_unmerged
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.
1685.1.80 by Wouter van Heyst
more code cleanup
12
#
1185.54.23 by Aaron Bentley
Added unit tests for find_unmerged
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
16
17
import os
18
19
3427.3.1 by John Arbash Meinel
Add bzrlib.missing.find_unmerged_mainline_revisions
20
from bzrlib import (
21
    missing,
22
    tests,
23
    )
2490.1.2 by John Arbash Meinel
Cleanup according to PEP8 and some other small whitespace fixes
24
from bzrlib.missing import (
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
25
    iter_log_revisions,
26
    )
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
27
from bzrlib.tests import TestCaseWithTransport
28
from bzrlib.workingtree import WorkingTree
29
1534.4.28 by Robert Collins
first cut at merge from integration.
30
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
31
class TestMissing(TestCaseWithTransport):
32
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
33
    def assertUnmerged(self, expected, source, target, restrict='all'):
34
        unmerged = missing.find_unmerged(source, target, restrict=restrict)
35
        self.assertEqual(expected, unmerged)
36
            
1185.54.23 by Aaron Bentley
Added unit tests for find_unmerged
37
    def test_find_unmerged(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
38
        original_tree = self.make_branch_and_tree('original')
39
        original = original_tree.branch
40
        puller_tree = self.make_branch_and_tree('puller')
41
        puller = puller_tree.branch
42
        merger_tree = self.make_branch_and_tree('merger')
43
        merger = merger_tree.branch
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
44
        self.assertUnmerged(([], []), original, puller)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
45
        original_tree.commit('a', rev_id='a')
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
46
        self.assertUnmerged(([(1, 'a')], []), original, puller)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
47
        puller_tree.pull(original)
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
48
        self.assertUnmerged(([], []), original, puller)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
49
        merger_tree.pull(original)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
50
        original_tree.commit('b', rev_id='b')
51
        original_tree.commit('c', rev_id='c')
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
52
        self.assertUnmerged(([(2, 'b'), (3, 'c')], []),
53
                            original, puller)
1185.54.23 by Aaron Bentley
Added unit tests for find_unmerged
54
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
55
        puller_tree.pull(original)
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
56
        self.assertUnmerged(([], []), original, puller)
57
        self.assertUnmerged(([(2, 'b'), (3, 'c')], []),
58
                            original, merger)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
59
        merger_tree.merge_from_branch(original)
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
60
        self.assertUnmerged(([(2, 'b'), (3, 'c')], []),
61
                            original, merger)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
62
        merger_tree.commit('d', rev_id='d')
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
63
        self.assertUnmerged(([], [(2, 'd')]), original, merger)
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
64
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
65
    def test_iter_log_revisions(self):
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
66
        base_tree = self.make_branch_and_tree('base')
67
        self.build_tree(['base/a'])
68
        base_tree.add(['a'], ['a-id'])
69
        base_tree.commit('add a', rev_id='b-1')
70
71
        child_tree = base_tree.bzrdir.sprout('child').open_workingtree()
72
73
        self.build_tree(['child/b'])
74
        child_tree.add(['b'], ['b-id'])
75
        child_tree.commit('adding b', rev_id='c-2')
76
77
        child_tree.remove(['a'])
78
        child_tree.commit('removing a', rev_id='c-3')
79
80
        self.build_tree_contents([('child/b', 'new contents for b\n')])
81
        child_tree.commit('modifying b', rev_id='c-4')
82
83
        child_tree.rename_one('b', 'c')
84
        child_tree.commit('rename b=>c', rev_id='c-5')
85
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
86
        base_extra, child_extra = missing.find_unmerged(base_tree.branch,
87
                                                        child_tree.branch)
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
88
        results = list(iter_log_revisions(base_extra, 
89
                            base_tree.branch.repository,
90
                            verbose=True))
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
91
        self.assertEqual([], results)
92
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
93
        results = list(iter_log_revisions(child_extra,
94
                            child_tree.branch.repository,
95
                            verbose=True))
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
96
        self.assertEqual(4, len(results))
97
98
        r0,r1,r2,r3 = results
99
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
100
        self.assertEqual((2, 'c-2'), (r0.revno, r0.rev.revision_id))
101
        self.assertEqual((3, 'c-3'), (r1.revno, r1.rev.revision_id))
102
        self.assertEqual((4, 'c-4'), (r2.revno, r2.rev.revision_id))
103
        self.assertEqual((5, 'c-5'), (r3.revno, r3.rev.revision_id))
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
104
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
105
        delta0 = r0.delta
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
106
        self.assertNotEqual(None, delta0)
107
        self.assertEqual([('b', 'b-id', 'file')], delta0.added)
108
        self.assertEqual([], delta0.removed)
109
        self.assertEqual([], delta0.renamed)
110
        self.assertEqual([], delta0.modified)
111
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
112
        delta1 = r1.delta
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
113
        self.assertNotEqual(None, delta1)
114
        self.assertEqual([], delta1.added)
115
        self.assertEqual([('a', 'a-id', 'file')], delta1.removed)
116
        self.assertEqual([], delta1.renamed)
117
        self.assertEqual([], delta1.modified)
118
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
119
        delta2 = r2.delta
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
120
        self.assertNotEqual(None, delta2)
121
        self.assertEqual([], delta2.added)
122
        self.assertEqual([], delta2.removed)
123
        self.assertEqual([], delta2.renamed)
124
        self.assertEqual([('b', 'b-id', 'file', True, False)], delta2.modified)
125
2466.8.1 by Kent Gibson
Reworked LogFormatter API to simplify extending the attributes of the revision being logged. Added support for begin_log() and end_log() hooks in LogFormatters.
126
        delta3 = r3.delta
2204.1.1 by John Arbash Meinel
'bzr missing -v' was showing adds as deletes.
127
        self.assertNotEqual(None, delta3)
128
        self.assertEqual([], delta3.added)
129
        self.assertEqual([], delta3.removed)
130
        self.assertEqual([('b', 'c', 'b-id', 'file', False, False)],
131
                         delta3.renamed)
132
        self.assertEqual([], delta3.modified)
3427.3.1 by John Arbash Meinel
Add bzrlib.missing.find_unmerged_mainline_revisions
133
134
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
135
class TestFindUnmerged(tests.TestCaseWithTransport):
3427.3.1 by John Arbash Meinel
Add bzrlib.missing.find_unmerged_mainline_revisions
136
137
    def assertUnmerged(self, local, remote, local_branch, remote_branch,
138
                       restrict):
139
        """Check the output of find_unmerged_mainline_revisions"""
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
140
        local_extra, remote_extra = missing.find_unmerged(
3427.3.1 by John Arbash Meinel
Add bzrlib.missing.find_unmerged_mainline_revisions
141
                                        local_branch, remote_branch, restrict)
142
        self.assertEqual(local, local_extra)
143
        self.assertEqual(remote, remote_extra)
144
145
    def test_same_branch(self):
146
        tree = self.make_branch_and_tree('tree')
147
        rev1 = tree.commit('one')
148
        tree.lock_read()
149
        self.addCleanup(tree.unlock)
150
        self.assertUnmerged([], [], tree.branch, tree.branch, 'all')
151
152
    def test_one_ahead(self):
153
        tree = self.make_branch_and_tree('tree')
154
        rev1 = tree.commit('one')
155
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
156
        rev2 = tree2.commit('two')
157
        tree.lock_read()
158
        self.addCleanup(tree.unlock)
159
        tree2.lock_read()
160
        self.addCleanup(tree2.unlock)
161
        self.assertUnmerged([], [(2, rev2)], tree.branch, tree2.branch, 'all')
162
        self.assertUnmerged([(2, rev2)], [], tree2.branch, tree.branch, 'all')
163
164
    def test_restrict(self):
165
        tree = self.make_branch_and_tree('tree')
166
        rev1 = tree.commit('one')
167
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
168
        rev2 = tree2.commit('two')
169
        tree.lock_read()
170
        self.addCleanup(tree.unlock)
171
        tree2.lock_read()
172
        self.addCleanup(tree2.unlock)
173
        self.assertUnmerged([], [(2, rev2)], tree.branch, tree2.branch, 'all')
174
        self.assertUnmerged([], None, tree.branch, tree2.branch, 'local')
175
        self.assertUnmerged(None, [(2, rev2)], tree.branch, tree2.branch,
176
                                               'remote')
177
3427.3.2 by John Arbash Meinel
switch find_unmerged to use the new function
178
    def test_merged(self):
179
        tree = self.make_branch_and_tree('tree')
180
        rev1 = tree.commit('one')
181
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
182
        rev2 = tree2.commit('two')
183
        rev3 = tree2.commit('three')
184
        tree.merge_from_branch(tree2.branch)
185
        rev4 = tree.commit('four')
186
187
        tree.lock_read()
188
        self.addCleanup(tree.unlock)
189
        tree2.lock_read()
190
        self.addCleanup(tree2.unlock)
191
192
        self.assertUnmerged([(2, rev4)], [], tree.branch, tree2.branch, 'all')