41
50
tree.commit('rev1')
53
def _setup_uncommitted(self, same_revision=False):
54
tree = self._setup_tree()
55
to_branch = tree.controldir.sprout('branch-2').open_branch()
56
self.build_tree(['branch-1/file-2'])
61
checkout = tree.branch.create_checkout('checkout',
62
lightweight=self.lightweight)
63
self.build_tree(['checkout/file-3'])
64
checkout.add('file-3')
65
return checkout, to_branch
67
def test_switch_store_uncommitted(self):
68
"""Test switch updates tree and stores uncommitted changes."""
69
checkout, to_branch = self._setup_uncommitted()
70
self.assertPathDoesNotExist('checkout/file-1')
71
self.assertPathExists('checkout/file-2')
72
switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
73
self.assertPathExists('checkout/file-1')
74
self.assertPathDoesNotExist('checkout/file-2')
75
self.assertPathDoesNotExist('checkout/file-3')
77
def test_switch_restore_uncommitted(self):
78
"""Test switch updates tree and restores uncommitted changes."""
79
checkout, to_branch = self._setup_uncommitted()
80
old_branch = self._master_if_present(checkout.branch)
81
self.assertPathDoesNotExist('checkout/file-1')
82
self.assertPathExists('checkout/file-2')
83
self.assertPathExists('checkout/file-3')
84
switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
85
checkout = workingtree.WorkingTree.open('checkout')
86
switch.switch(checkout.controldir, old_branch, store_uncommitted=True)
87
self.assertPathDoesNotExist('checkout/file-1')
88
self.assertPathExists('checkout/file-2')
89
self.assertPathExists('checkout/file-3')
91
def test_switch_restore_uncommitted_same_revision(self):
92
"""Test switch updates tree and restores uncommitted changes."""
93
checkout, to_branch = self._setup_uncommitted(same_revision=True)
94
old_branch = self._master_if_present(checkout.branch)
95
switch.switch(checkout.controldir, to_branch, store_uncommitted=True)
96
checkout = workingtree.WorkingTree.open('checkout')
97
switch.switch(checkout.controldir, old_branch, store_uncommitted=True)
98
self.assertPathExists('checkout/file-3')
44
100
def test_switch_updates(self):
45
101
"""Test switch updates tree and keeps uncommitted changes."""
46
tree = self._setup_tree()
47
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
48
self.build_tree(['branch-1/file-2'])
52
checkout = tree.branch.create_checkout('checkout',
53
lightweight=self.lightweight)
54
self.build_tree(['checkout/file-3'])
55
checkout.add('file-3')
56
self.failIfExists('checkout/file-1')
57
self.failUnlessExists('checkout/file-2')
58
switch.switch(checkout.bzrdir, to_branch)
59
self.failUnlessExists('checkout/file-1')
60
self.failIfExists('checkout/file-2')
61
self.failUnlessExists('checkout/file-3')
102
checkout, to_branch = self._setup_uncommitted()
103
self.assertPathDoesNotExist('checkout/file-1')
104
self.assertPathExists('checkout/file-2')
105
switch.switch(checkout.controldir, to_branch)
106
self.assertPathExists('checkout/file-1')
107
self.assertPathDoesNotExist('checkout/file-2')
108
self.assertPathExists('checkout/file-3')
63
110
def test_switch_after_branch_moved(self):
64
111
"""Test switch after the branch is moved."""
65
112
tree = self._setup_tree()
66
113
checkout = tree.branch.create_checkout('checkout',
67
lightweight=self.lightweight)
114
lightweight=self.lightweight)
68
115
self.build_tree(['branch-1/file-2'])
69
116
tree.add('file-2')
70
117
tree.remove('file-1')
71
118
tree.commit('rev2')
72
119
self.build_tree(['checkout/file-3'])
73
120
checkout.add('file-3')
74
checkout_dir = checkout.bzrdir
75
121
# rename the branch on disk, the checkout object is now invalid.
76
122
os.rename('branch-1', 'branch-2')
77
123
to_branch = branch.Branch.open('branch-2')
78
124
# Check fails without --force
79
125
err = self.assertRaises(
80
(errors.BzrCommandError, errors.NotBranchError),
81
switch.switch, checkout.bzrdir, to_branch)
82
if isinstance(err, errors.BzrCommandError):
126
(errors.CommandError, errors.NotBranchError),
127
switch.switch, checkout.controldir, to_branch)
128
if isinstance(err, errors.CommandError):
83
129
self.assertContainsRe(str(err),
84
'Unable to connect to current master branch .*'
85
'To switch anyway, use --force.')
86
switch.switch(checkout.bzrdir, to_branch, force=True)
87
self.failIfExists('checkout/file-1')
88
self.failUnlessExists('checkout/file-2')
89
self.failUnlessExists('checkout/file-3')
130
'Unable to connect to current master branch .*'
131
'To switch anyway, use --force.')
132
switch.switch(checkout.controldir, to_branch, force=True)
133
self.assertPathDoesNotExist('checkout/file-1')
134
self.assertPathExists('checkout/file-2')
135
self.assertPathExists('checkout/file-3')
91
137
def test_switch_when_pending_merges(self):
92
138
"""Test graceful failure if pending merges are outstanding."""
93
139
# Create 2 branches and a checkout
94
140
tree = self._setup_tree()
95
tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
141
tree2 = tree.controldir.sprout('branch-2').open_workingtree()
96
142
checkout = tree.branch.create_checkout('checkout',
97
lightweight=self.lightweight)
143
lightweight=self.lightweight)
98
144
# Change tree2 and merge it into the checkout without committing
99
145
self.build_tree(['branch-2/file-2'])
100
146
tree2.add('file-2')
101
147
tree2.commit('rev2')
102
148
checkout.merge_from_branch(tree2.branch)
103
149
# Check the error reporting is as expected
104
err = self.assertRaises(errors.BzrCommandError,
105
switch.switch, checkout.bzrdir, tree2.branch)
150
err = self.assertRaises(errors.CommandError,
151
switch.switch, checkout.controldir, tree2.branch)
106
152
self.assertContainsRe(str(err),
107
"Pending merges must be committed or reverted before using switch")
153
"Pending merges must be committed or reverted before using switch")
109
155
def test_switch_with_revision(self):
110
156
"""Test switch when a revision is given."""
112
158
tree = self.make_branch_and_tree('branch-1')
113
159
self.build_tree(['branch-1/file-1'])
114
160
tree.add('file-1')
115
tree.commit(rev_id='rev1', message='rev1')
161
tree.commit(rev_id=b'rev1', message='rev1')
116
162
self.build_tree(['branch-1/file-2'])
117
163
tree.add('file-2')
118
tree.commit(rev_id='rev2', message='rev2')
164
tree.commit(rev_id=b'rev2', message='rev2')
119
165
# Check it out and switch to revision 1
120
166
checkout = tree.branch.create_checkout('checkout',
121
lightweight=self.lightweight)
122
switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1")
123
self.failUnlessExists('checkout/file-1')
124
self.failIfExists('checkout/file-2')
167
lightweight=self.lightweight)
168
switch.switch(checkout.controldir, tree.branch, revision_id=b"rev1")
169
self.assertPathExists('checkout/file-1')
170
self.assertPathDoesNotExist('checkout/file-2')
126
172
def test_switch_changing_root_id(self):
127
173
tree = self._setup_tree()
128
174
tree2 = self.make_branch_and_tree('tree-2')
129
tree2.set_root_id('custom-root-id')
175
tree2.set_root_id(b'custom-root-id')
130
176
self.build_tree(['tree-2/file-2'])
131
177
tree2.add(['file-2'])
132
178
tree2.commit('rev1b')
133
179
checkout = tree.branch.create_checkout('checkout',
134
lightweight=self.lightweight)
135
switch.switch(checkout.bzrdir, tree2.branch)
136
self.assertEqual('custom-root-id', tree2.get_root_id())
180
lightweight=self.lightweight)
181
switch.switch(checkout.controldir, tree2.branch)
182
self.assertEqual(b'custom-root-id', tree2.path2id(''))
138
184
def test_switch_configurable_file_merger(self):
139
185
class DummyMerger(_mod_merge.ConfigurableFileMerger):
174
220
checkout.commit(message='local only commit', local=True)
175
221
self.build_tree(['checkout/file-4'])
176
222
# Check the error reporting is as expected
177
err = self.assertRaises(errors.BzrCommandError,
178
switch.switch, checkout.bzrdir, to_branch)
223
err = self.assertRaises(errors.CommandError,
224
switch.switch, checkout.controldir, to_branch)
179
225
self.assertContainsRe(str(err),
180
'Cannot switch as local commits found in the checkout.')
226
'Cannot switch as local commits found in the checkout.')
181
227
# Check all is ok when force is given
182
self.failIfExists('checkout/file-1')
183
self.failUnlessExists('checkout/file-2')
184
switch.switch(checkout.bzrdir, to_branch, force=True)
185
self.failUnlessExists('checkout/file-1')
186
self.failIfExists('checkout/file-2')
187
self.failIfExists('checkout/file-3')
188
self.failUnlessExists('checkout/file-4')
228
self.assertPathDoesNotExist('checkout/file-1')
229
self.assertPathExists('checkout/file-2')
230
switch.switch(checkout.controldir, to_branch, force=True)
231
self.assertPathExists('checkout/file-1')
232
self.assertPathDoesNotExist('checkout/file-2')
233
self.assertPathDoesNotExist('checkout/file-3')
234
self.assertPathExists('checkout/file-4')
189
235
# Check that the checkout is a true mirror of the bound branch
190
236
self.assertEqual(to_branch.last_revision_info(),
191
237
checkout.branch.last_revision_info())