51
31
tree.commit('rev1')
54
def _setup_uncommitted(self, same_revision=False):
55
tree = self._setup_tree()
56
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
57
self.build_tree(['branch-1/file-2'])
62
checkout = tree.branch.create_checkout('checkout',
63
lightweight=self.lightweight)
64
self.build_tree(['checkout/file-3'])
65
checkout.add('file-3')
66
return checkout, to_branch
68
def test_switch_store_uncommitted(self):
69
"""Test switch updates tree and stores uncommitted changes."""
70
checkout, to_branch = self._setup_uncommitted()
71
self.assertPathDoesNotExist('checkout/file-1')
72
self.assertPathExists('checkout/file-2')
73
switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
74
self.assertPathExists('checkout/file-1')
75
self.assertPathDoesNotExist('checkout/file-2')
76
self.assertPathDoesNotExist('checkout/file-3')
78
def test_switch_restore_uncommitted(self):
79
"""Test switch updates tree and restores uncommitted changes."""
80
checkout, to_branch = self._setup_uncommitted()
81
old_branch = self._master_if_present(checkout.branch)
82
self.assertPathDoesNotExist('checkout/file-1')
83
self.assertPathExists('checkout/file-2')
84
self.assertPathExists('checkout/file-3')
85
switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
86
checkout = workingtree.WorkingTree.open('checkout')
87
switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True)
88
self.assertPathDoesNotExist('checkout/file-1')
89
self.assertPathExists('checkout/file-2')
90
self.assertPathExists('checkout/file-3')
92
def test_switch_restore_uncommitted_same_revision(self):
93
"""Test switch updates tree and restores uncommitted changes."""
94
checkout, to_branch = self._setup_uncommitted(same_revision=True)
95
old_branch = self._master_if_present(checkout.branch)
96
switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True)
97
checkout = workingtree.WorkingTree.open('checkout')
98
switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True)
99
self.assertPathExists('checkout/file-3')
101
34
def test_switch_updates(self):
102
35
"""Test switch updates tree and keeps uncommitted changes."""
103
checkout, to_branch = self._setup_uncommitted()
104
self.assertPathDoesNotExist('checkout/file-1')
105
self.assertPathExists('checkout/file-2')
36
tree = self._setup_tree()
37
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
38
self.build_tree(['branch-1/file-2'])
42
checkout = tree.branch.create_checkout('checkout', lightweight=True)
43
self.build_tree(['checkout/file-3'])
44
checkout.add('file-3')
45
self.failIfExists('checkout/file-1')
46
self.failUnlessExists('checkout/file-2')
106
47
switch.switch(checkout.bzrdir, to_branch)
107
self.assertPathExists('checkout/file-1')
108
self.assertPathDoesNotExist('checkout/file-2')
109
self.assertPathExists('checkout/file-3')
48
self.failUnlessExists('checkout/file-1')
49
self.failIfExists('checkout/file-2')
50
self.failUnlessExists('checkout/file-3')
111
52
def test_switch_after_branch_moved(self):
112
53
"""Test switch after the branch is moved."""
113
54
tree = self._setup_tree()
114
checkout = tree.branch.create_checkout('checkout',
115
lightweight=self.lightweight)
55
checkout = tree.branch.create_checkout('checkout', lightweight=True)
116
56
self.build_tree(['branch-1/file-2'])
117
57
tree.add('file-2')
118
58
tree.remove('file-1')
119
59
tree.commit('rev2')
120
self.build_tree(['checkout/file-3'])
121
checkout.add('file-3')
122
checkout_dir = checkout.bzrdir
123
# rename the branch on disk, the checkout object is now invalid.
124
60
os.rename('branch-1', 'branch-2')
125
61
to_branch = branch.Branch.open('branch-2')
126
# Check fails without --force
127
err = self.assertRaises(
128
(errors.BzrCommandError, errors.NotBranchError),
129
switch.switch, checkout.bzrdir, to_branch)
130
if isinstance(err, errors.BzrCommandError):
131
self.assertContainsRe(str(err),
132
'Unable to connect to current master branch .*'
133
'To switch anyway, use --force.')
134
switch.switch(checkout.bzrdir, to_branch, force=True)
135
self.assertPathDoesNotExist('checkout/file-1')
136
self.assertPathExists('checkout/file-2')
137
self.assertPathExists('checkout/file-3')
62
self.build_tree(['checkout/file-3'])
63
checkout.add('file-3')
64
switch.switch(checkout.bzrdir, to_branch)
65
self.failIfExists('checkout/file-1')
66
self.failUnlessExists('checkout/file-2')
67
self.failUnlessExists('checkout/file-3')
69
def test_switch_on_heavy_checkout(self):
70
"""Test graceful failure on heavyweight checkouts."""
71
tree = self._setup_tree()
72
checkout = tree.branch.create_checkout('checkout-1', lightweight=False)
73
branch2 = self.make_branch('branch-2')
74
err = self.assertRaises(errors.BzrCommandError,
75
switch.switch, checkout.bzrdir, branch2)
76
self.assertContainsRe(str(err),
77
"The switch command can only be used on a lightweight checkout")
139
79
def test_switch_when_pending_merges(self):
140
80
"""Test graceful failure if pending merges are outstanding."""
141
81
# Create 2 branches and a checkout
142
82
tree = self._setup_tree()
143
83
tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
144
checkout = tree.branch.create_checkout('checkout',
145
lightweight=self.lightweight)
84
checkout = tree.branch.create_checkout('checkout', lightweight=True)
146
85
# Change tree2 and merge it into the checkout without committing
147
86
self.build_tree(['branch-2/file-2'])
148
87
tree2.add('file-2')
153
92
switch.switch, checkout.bzrdir, tree2.branch)
154
93
self.assertContainsRe(str(err),
155
94
"Pending merges must be committed or reverted before using switch")
157
def test_switch_with_revision(self):
158
"""Test switch when a revision is given."""
159
# Create a tree with 2 revisions
160
tree = self.make_branch_and_tree('branch-1')
161
self.build_tree(['branch-1/file-1'])
163
tree.commit(rev_id='rev1', message='rev1')
164
self.build_tree(['branch-1/file-2'])
166
tree.commit(rev_id='rev2', message='rev2')
167
# Check it out and switch to revision 1
168
checkout = tree.branch.create_checkout('checkout',
169
lightweight=self.lightweight)
170
switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1")
171
self.assertPathExists('checkout/file-1')
172
self.assertPathDoesNotExist('checkout/file-2')
174
def test_switch_changing_root_id(self):
175
tree = self._setup_tree()
176
tree2 = self.make_branch_and_tree('tree-2')
177
tree2.set_root_id('custom-root-id')
178
self.build_tree(['tree-2/file-2'])
179
tree2.add(['file-2'])
180
tree2.commit('rev1b')
181
checkout = tree.branch.create_checkout('checkout',
182
lightweight=self.lightweight)
183
switch.switch(checkout.bzrdir, tree2.branch)
184
self.assertEqual('custom-root-id', tree2.get_root_id())
186
def test_switch_configurable_file_merger(self):
187
class DummyMerger(_mod_merge.ConfigurableFileMerger):
190
_mod_merge.Merger.hooks.install_named_hook(
191
'merge_file_content', DummyMerger,
193
foo = self.make_branch('foo')
194
checkout = foo.create_checkout('checkout', lightweight=True)
195
self.build_tree_contents([('checkout/file', 'a')])
198
bar = foo.bzrdir.sprout('bar').open_workingtree()
199
self.build_tree_contents([('bar/file', 'b')])
201
self.build_tree_contents([('checkout/file', 'c')])
202
switch.switch(checkout.bzrdir, bar.branch)
205
class TestSwitchHeavyweight(TestSwitch):
208
super(TestSwitchHeavyweight, self).setUp()
209
self.lightweight = False
211
def test_switch_with_local_commits(self):
212
"""Test switch complains about local commits unless --force given."""
213
tree = self._setup_tree()
214
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
215
self.build_tree(['branch-1/file-2'])
217
tree.remove('file-1')
219
checkout = tree.branch.create_checkout('checkout')
220
self.build_tree(['checkout/file-3'])
221
checkout.add('file-3')
222
checkout.commit(message='local only commit', local=True)
223
self.build_tree(['checkout/file-4'])
224
# Check the error reporting is as expected
225
err = self.assertRaises(errors.BzrCommandError,
226
switch.switch, checkout.bzrdir, to_branch)
227
self.assertContainsRe(str(err),
228
'Cannot switch as local commits found in the checkout.')
229
# Check all is ok when force is given
230
self.assertPathDoesNotExist('checkout/file-1')
231
self.assertPathExists('checkout/file-2')
232
switch.switch(checkout.bzrdir, to_branch, force=True)
233
self.assertPathExists('checkout/file-1')
234
self.assertPathDoesNotExist('checkout/file-2')
235
self.assertPathDoesNotExist('checkout/file-3')
236
self.assertPathExists('checkout/file-4')
237
# Check that the checkout is a true mirror of the bound branch
238
self.assertEqual(to_branch.last_revision_info(),
239
checkout.branch.last_revision_info())