1
# Copyright (C) 2007, 2008, 2009, 2011, 2012 Canonical Ltd
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
branch as _mod_branch,
26
from breezy.bzr import (
27
branch as _mod_bzrbranch,
32
class TestReconfigure(tests.TestCaseWithTransport):
34
def test_tree_to_branch(self):
35
tree = self.make_branch_and_tree('tree')
36
reconfiguration = reconfigure.Reconfigure.to_branch(tree.controldir)
37
reconfiguration.apply()
38
self.assertRaises(errors.NoWorkingTree, workingtree.WorkingTree.open,
41
def test_modified_tree_to_branch(self):
42
tree = self.make_branch_and_tree('tree')
43
self.build_tree(['tree/file'])
45
reconfiguration = reconfigure.Reconfigure.to_branch(tree.controldir)
46
self.assertRaises(errors.UncommittedChanges, reconfiguration.apply)
47
reconfiguration.apply(force=True)
48
self.assertRaises(errors.NoWorkingTree, workingtree.WorkingTree.open,
51
def test_tree_with_pending_merge_to_branch(self):
52
tree = self.make_branch_and_tree('tree')
53
tree.commit('unchanged')
54
other_tree = tree.controldir.sprout('other').open_workingtree()
55
other_tree.commit('mergeable commit')
56
tree.merge_from_branch(other_tree.branch)
57
reconfiguration = reconfigure.Reconfigure.to_branch(tree.controldir)
58
self.assertRaises(errors.UncommittedChanges, reconfiguration.apply)
59
reconfiguration.apply(force=True)
60
self.assertRaises(errors.NoWorkingTree, workingtree.WorkingTree.open,
63
def test_branch_to_branch(self):
64
branch = self.make_branch('branch')
65
self.assertRaises(reconfigure.AlreadyBranch,
66
reconfigure.Reconfigure.to_branch, branch.controldir)
68
def test_repo_to_branch(self):
69
repo = self.make_repository('repo')
70
reconfiguration = reconfigure.Reconfigure.to_branch(repo.controldir)
71
reconfiguration.apply()
73
def test_checkout_to_branch(self):
74
branch = self.make_branch('branch')
75
checkout = branch.create_checkout('checkout')
76
reconfiguration = reconfigure.Reconfigure.to_branch(checkout.controldir)
77
reconfiguration.apply()
78
reconfigured = controldir.ControlDir.open('checkout').open_branch()
79
self.assertIs(None, reconfigured.get_bound_location())
81
def prepare_lightweight_checkout_to_branch(self):
82
branch = self.make_branch('branch')
83
checkout = branch.create_checkout('checkout', lightweight=True)
84
checkout.commit('first commit', rev_id=b'rev1')
85
reconfiguration = reconfigure.Reconfigure.to_branch(checkout.controldir)
86
return reconfiguration, checkout
88
def test_lightweight_checkout_to_branch(self):
89
reconfiguration, checkout = \
90
self.prepare_lightweight_checkout_to_branch()
91
reconfiguration.apply()
92
checkout_branch = checkout.controldir.open_branch()
93
self.assertEqual(checkout_branch.controldir.root_transport.base,
94
checkout.controldir.root_transport.base)
95
self.assertEqual('rev1', checkout_branch.last_revision())
96
repo = checkout.controldir.open_repository()
97
repo.get_revision('rev1')
99
def test_lightweight_checkout_to_branch_tags(self):
100
reconfiguration, checkout = \
101
self.prepare_lightweight_checkout_to_branch()
102
checkout.branch.tags.set_tag('foo', 'bar')
103
reconfiguration.apply()
104
checkout_branch = checkout.controldir.open_branch()
105
self.assertEqual('bar', checkout_branch.tags.lookup_tag('foo'))
107
def prepare_lightweight_checkout_to_checkout(self):
108
branch = self.make_branch('branch')
109
checkout = branch.create_checkout('checkout', lightweight=True)
110
reconfiguration = reconfigure.Reconfigure.to_checkout(checkout.controldir)
111
return reconfiguration, checkout
113
def test_lightweight_checkout_to_checkout(self):
114
reconfiguration, checkout = \
115
self.prepare_lightweight_checkout_to_checkout()
116
reconfiguration.apply()
117
checkout_branch = checkout.controldir.open_branch()
118
self.assertIsNot(checkout_branch.get_bound_location(), None)
120
def test_lightweight_checkout_to_checkout_tags(self):
121
reconfiguration, checkout = \
122
self.prepare_lightweight_checkout_to_checkout()
123
checkout.branch.tags.set_tag('foo', 'bar')
124
reconfiguration.apply()
125
checkout_branch = checkout.controldir.open_branch()
126
self.assertEqual('bar', checkout_branch.tags.lookup_tag('foo'))
128
def test_lightweight_conversion_uses_shared_repo(self):
129
parent = self.make_branch('parent')
130
shared_repo = self.make_repository('repo', shared=True)
131
checkout = parent.create_checkout('repo/checkout', lightweight=True)
132
reconfigure.Reconfigure.to_tree(checkout.controldir).apply()
133
checkout_repo = checkout.controldir.open_branch().repository
134
self.assertEqual(shared_repo.controldir.root_transport.base,
135
checkout_repo.controldir.root_transport.base)
137
def test_branch_to_tree(self):
138
branch = self.make_branch('branch')
139
reconfiguration=reconfigure.Reconfigure.to_tree(branch.controldir)
140
reconfiguration.apply()
141
branch.controldir.open_workingtree()
143
def test_tree_to_tree(self):
144
tree = self.make_branch_and_tree('tree')
145
self.assertRaises(reconfigure.AlreadyTree,
146
reconfigure.Reconfigure.to_tree, tree.controldir)
148
def test_select_bind_location(self):
149
branch = self.make_branch('branch')
150
reconfiguration = reconfigure.Reconfigure(branch.controldir)
151
self.assertRaises(reconfigure.NoBindLocation,
152
reconfiguration._select_bind_location)
153
branch.set_parent('http://parent')
154
reconfiguration = reconfigure.Reconfigure(branch.controldir)
155
self.assertEqual('http://parent',
156
reconfiguration._select_bind_location())
157
branch.set_push_location('sftp://push')
158
reconfiguration = reconfigure.Reconfigure(branch.controldir)
159
self.assertEqual('sftp://push',
160
reconfiguration._select_bind_location())
163
branch.set_bound_location('bzr://foo/old-bound')
164
branch.set_bound_location(None)
167
reconfiguration = reconfigure.Reconfigure(branch.controldir)
168
self.assertEqual('bzr://foo/old-bound',
169
reconfiguration._select_bind_location())
170
branch.set_bound_location('bzr://foo/cur-bound')
171
reconfiguration = reconfigure.Reconfigure(branch.controldir)
172
self.assertEqual('bzr://foo/cur-bound',
173
reconfiguration._select_bind_location())
174
reconfiguration.new_bound_location = 'ftp://user-specified'
175
self.assertEqual('ftp://user-specified',
176
reconfiguration._select_bind_location())
178
def test_select_reference_bind_location(self):
179
branch = self.make_branch('branch')
180
checkout = branch.create_checkout('checkout', lightweight=True)
181
reconfiguration = reconfigure.Reconfigure(checkout.controldir)
182
self.assertEqual(branch.base,
183
reconfiguration._select_bind_location())
185
def test_tree_to_checkout(self):
186
# A tree with no related branches and no supplied bind location cannot
188
parent = self.make_branch('parent')
190
tree = self.make_branch_and_tree('tree')
191
reconfiguration = reconfigure.Reconfigure.to_checkout(tree.controldir)
192
self.assertRaises(reconfigure.NoBindLocation, reconfiguration.apply)
193
# setting a parent allows it to become a checkout
194
tree.branch.set_parent(parent.base)
195
reconfiguration = reconfigure.Reconfigure.to_checkout(tree.controldir)
196
reconfiguration.apply()
197
# supplying a location allows it to become a checkout
198
tree2 = self.make_branch_and_tree('tree2')
199
reconfiguration = reconfigure.Reconfigure.to_checkout(tree2.controldir,
201
reconfiguration.apply()
203
def test_tree_to_lightweight_checkout(self):
204
# A tree with no related branches and no supplied bind location cannot
206
parent = self.make_branch('parent')
208
tree = self.make_branch_and_tree('tree')
209
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
211
self.assertRaises(reconfigure.NoBindLocation, reconfiguration.apply)
212
# setting a parent allows it to become a checkout
213
tree.branch.set_parent(parent.base)
214
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
216
reconfiguration.apply()
217
# supplying a location allows it to become a checkout
218
tree2 = self.make_branch_and_tree('tree2')
219
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
220
tree2.controldir, parent.base)
221
reconfiguration.apply()
223
def test_checkout_to_checkout(self):
224
parent = self.make_branch('parent')
225
checkout = parent.create_checkout('checkout')
226
self.assertRaises(reconfigure.AlreadyCheckout,
227
reconfigure.Reconfigure.to_checkout, checkout.controldir)
229
def make_unsynced_checkout(self):
230
parent = self.make_branch('parent')
231
checkout = parent.create_checkout('checkout')
232
checkout.commit('test', rev_id=b'new-commit', local=True)
233
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
235
return checkout, parent, reconfiguration
237
def test_unsynced_checkout_to_lightweight(self):
238
checkout, parent, reconfiguration = self.make_unsynced_checkout()
239
self.assertRaises(reconfigure.UnsyncedBranches, reconfiguration.apply)
241
def test_synced_checkout_to_lightweight(self):
242
checkout, parent, reconfiguration = self.make_unsynced_checkout()
243
parent.pull(checkout.branch)
244
reconfiguration.apply()
245
wt = checkout.controldir.open_workingtree()
246
self.assertTrue(parent.repository.has_same_location(
247
wt.branch.repository))
248
parent.repository.get_revision('new-commit')
249
self.assertRaises(errors.NoRepositoryPresent,
250
checkout.controldir.open_repository)
252
def prepare_branch_to_lightweight_checkout(self):
253
parent = self.make_branch('parent')
254
child = parent.controldir.sprout('child').open_workingtree()
255
child.commit('test', rev_id=b'new-commit')
256
parent.pull(child.branch)
257
child.controldir.destroy_workingtree()
258
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
260
return parent, child, reconfiguration
262
def test_branch_to_lightweight_checkout(self):
263
parent, child, reconfiguration = \
264
self.prepare_branch_to_lightweight_checkout()
265
reconfiguration.apply()
266
self.assertTrue(reconfiguration._destroy_branch)
267
wt = child.controldir.open_workingtree()
268
self.assertTrue(parent.repository.has_same_location(
269
wt.branch.repository))
270
parent.repository.get_revision('new-commit')
271
self.assertRaises(errors.NoRepositoryPresent,
272
child.controldir.open_repository)
274
def test_branch_to_lightweight_checkout_failure(self):
275
parent, child, reconfiguration = \
276
self.prepare_branch_to_lightweight_checkout()
277
old_Repository_fetch = vf_repository.VersionedFileRepository.fetch
278
vf_repository.VersionedFileRepository.fetch = None
280
self.assertRaises(TypeError, reconfiguration.apply)
282
vf_repository.VersionedFileRepository.fetch = old_Repository_fetch
283
child = _mod_branch.Branch.open('child')
284
self.assertContainsRe(child.base, 'child/$')
286
def test_branch_to_lightweight_checkout_fetch_tags(self):
287
parent, child, reconfiguration = \
288
self.prepare_branch_to_lightweight_checkout()
289
child.branch.tags.set_tag('foo', 'bar')
290
reconfiguration.apply()
291
child = _mod_branch.Branch.open('child')
292
self.assertEqual('bar', parent.tags.lookup_tag('foo'))
294
def test_lightweight_checkout_to_lightweight_checkout(self):
295
parent = self.make_branch('parent')
296
checkout = parent.create_checkout('checkout', lightweight=True)
297
self.assertRaises(reconfigure.AlreadyLightweightCheckout,
298
reconfigure.Reconfigure.to_lightweight_checkout,
301
def test_repo_to_tree(self):
302
repo = self.make_repository('repo')
303
reconfiguration = reconfigure.Reconfigure.to_tree(repo.controldir)
304
reconfiguration.apply()
305
workingtree.WorkingTree.open('repo')
307
def test_shared_repo_to_lightweight_checkout(self):
308
repo = self.make_repository('repo', shared=True)
309
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
311
self.assertRaises(reconfigure.NoBindLocation, reconfiguration.apply)
312
branch = self.make_branch('branch')
313
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
314
repo.controldir, 'branch')
315
reconfiguration.apply()
316
workingtree.WorkingTree.open('repo')
317
repository.Repository.open('repo')
319
def test_unshared_repo_to_lightweight_checkout(self):
320
repo = self.make_repository('repo', shared=False)
321
branch = self.make_branch('branch')
322
reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
323
repo.controldir, 'branch')
324
reconfiguration.apply()
325
workingtree.WorkingTree.open('repo')
326
self.assertRaises(errors.NoRepositoryPresent,
327
repository.Repository.open, 'repo')
329
def test_standalone_to_use_shared(self):
330
self.build_tree(['root/'])
331
tree = self.make_branch_and_tree('root/tree')
332
tree.commit('Hello', rev_id=b'hello-id')
333
repo = self.make_repository('root', shared=True)
334
reconfiguration = reconfigure.Reconfigure.to_use_shared(tree.controldir)
335
reconfiguration.apply()
336
tree = workingtree.WorkingTree.open('root/tree')
337
self.assertTrue(repo.has_same_location(tree.branch.repository))
338
self.assertEqual('Hello', repo.get_revision('hello-id').message)
340
def add_dead_head(self, tree):
341
revno, revision_id = tree.branch.last_revision_info()
342
tree.commit('Dead head', rev_id=b'dead-head-id')
343
tree.branch.set_last_revision_info(revno, revision_id)
344
tree.set_last_revision(revision_id)
346
def test_standalone_to_use_shared_preserves_dead_heads(self):
347
self.build_tree(['root/'])
348
tree = self.make_branch_and_tree('root/tree')
349
self.add_dead_head(tree)
350
tree.commit('Hello', rev_id=b'hello-id')
351
repo = self.make_repository('root', shared=True)
352
reconfiguration = reconfigure.Reconfigure.to_use_shared(tree.controldir)
353
reconfiguration.apply()
354
tree = workingtree.WorkingTree.open('root/tree')
355
message = repo.get_revision('dead-head-id').message
356
self.assertEqual('Dead head', message)
358
def make_repository_tree(self):
359
self.build_tree(['root/'])
360
repo = self.make_repository('root', shared=True)
361
tree = self.make_branch_and_tree('root/tree')
362
reconfigure.Reconfigure.to_use_shared(tree.controldir).apply()
363
return workingtree.WorkingTree.open('root/tree')
365
def test_use_shared_to_use_shared(self):
366
tree = self.make_repository_tree()
367
self.assertRaises(reconfigure.AlreadyUsingShared,
368
reconfigure.Reconfigure.to_use_shared, tree.controldir)
370
def test_use_shared_to_standalone(self):
371
tree = self.make_repository_tree()
372
tree.commit('Hello', rev_id=b'hello-id')
373
reconfigure.Reconfigure.to_standalone(tree.controldir).apply()
374
tree = workingtree.WorkingTree.open('root/tree')
375
repo = tree.branch.repository
376
self.assertEqual(repo.controldir.root_transport.base,
377
tree.controldir.root_transport.base)
378
self.assertEqual('Hello', repo.get_revision('hello-id').message)
380
def test_use_shared_to_standalone_preserves_dead_heads(self):
381
tree = self.make_repository_tree()
382
self.add_dead_head(tree)
383
tree.commit('Hello', rev_id=b'hello-id')
384
reconfigure.Reconfigure.to_standalone(tree.controldir).apply()
385
tree = workingtree.WorkingTree.open('root/tree')
386
repo = tree.branch.repository
387
self.assertRaises(errors.NoSuchRevision, repo.get_revision,
390
def test_standalone_to_standalone(self):
391
tree = self.make_branch_and_tree('tree')
392
self.assertRaises(reconfigure.AlreadyStandalone,
393
reconfigure.Reconfigure.to_standalone, tree.controldir)
395
def make_unsynced_branch_reconfiguration(self):
396
parent = self.make_branch_and_tree('parent')
397
parent.commit('commit 1')
398
child = parent.controldir.sprout('child').open_workingtree()
399
child.commit('commit 2')
400
return reconfigure.Reconfigure.to_lightweight_checkout(child.controldir)
402
def test_unsynced_branch_to_lightweight_checkout_unforced(self):
403
reconfiguration = self.make_unsynced_branch_reconfiguration()
404
self.assertRaises(reconfigure.UnsyncedBranches, reconfiguration.apply)
406
def test_unsynced_branch_to_lightweight_checkout_forced(self):
407
reconfiguration = self.make_unsynced_branch_reconfiguration()
408
reconfiguration.apply(force=True)
410
def make_repository_with_without_trees(self, with_trees):
411
repo = self.make_repository('repo', shared=True)
412
repo.set_make_working_trees(with_trees)
415
def test_make_with_trees(self):
416
repo = self.make_repository_with_without_trees(False)
417
reconfiguration = reconfigure.Reconfigure.set_repository_trees(
418
repo.controldir, True)
419
reconfiguration.apply()
420
self.assertIs(True, repo.make_working_trees())
422
def test_make_without_trees(self):
423
repo = self.make_repository_with_without_trees(True)
424
reconfiguration = reconfigure.Reconfigure.set_repository_trees(
425
repo.controldir, False)
426
reconfiguration.apply()
427
self.assertIs(False, repo.make_working_trees())
429
def test_make_with_trees_already_with_trees(self):
430
repo = self.make_repository_with_without_trees(True)
431
e = self.assertRaises(reconfigure.AlreadyWithTrees,
432
reconfigure.Reconfigure.set_repository_trees, repo.controldir, True)
433
self.assertContainsRe(str(e),
434
r"Shared repository '.*' already creates working trees.")
436
def test_make_without_trees_already_no_trees(self):
437
repo = self.make_repository_with_without_trees(False)
438
e = self.assertRaises(reconfigure.AlreadyWithNoTrees,
439
reconfigure.Reconfigure.set_repository_trees, repo.controldir, False)
440
self.assertContainsRe(str(e),
441
r"Shared repository '.*' already doesn't create working trees.")
443
def test_repository_tree_reconfiguration_not_supported(self):
444
tree = self.make_branch_and_tree('tree')
445
e = self.assertRaises(reconfigure.ReconfigurationNotSupported,
446
reconfigure.Reconfigure.set_repository_trees, tree.controldir, None)
447
self.assertContainsRe(str(e),
448
r"Requested reconfiguration of '.*' is not supported.")
450
def test_lightweight_checkout_to_tree_preserves_reference_locations(self):
451
format = controldir.format_registry.make_controldir('1.9')
452
format.set_branch_format(_mod_bzrbranch.BzrBranchFormat8())
453
tree = self.make_branch_and_tree('tree', format=format)
454
tree.branch.set_reference_info('path', '../location', 'file_id')
455
checkout = tree.branch.create_checkout('checkout', lightweight=True)
456
reconfiguration = reconfigure.Reconfigure.to_tree(checkout.controldir)
457
reconfiguration.apply()
458
checkout_branch = checkout.controldir.open_branch()
459
self.assertEqual(('../location', b'file_id'),
460
checkout_branch.get_reference_info('path'))