def test_switch_after_branch_moved(self): """Test switch after the branch is moved.""" tree = self._setup_tree() checkout = tree.branch.create_checkout('checkout', lightweight=self.lightweight) self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') self.build_tree(['checkout/file-3']) checkout.add('file-3') checkout_dir = checkout.bzrdir # rename the branch on disk, the checkout object is now invalid. os.rename('branch-1', 'branch-2') to_branch = branch.Branch.open('branch-2') # Check fails without --force err = self.assertRaises( (errors.BzrCommandError, errors.NotBranchError), switch.switch, checkout.bzrdir, to_branch) if isinstance(err, errors.BzrCommandError): self.assertContainsRe(str(err), 'Unable to connect to current master branch .*' 'To switch anyway, use --force.') switch.switch(checkout.bzrdir, to_branch, force=True) self.assertPathDoesNotExist('checkout/file-1') self.assertPathExists('checkout/file-2') self.assertPathExists('checkout/file-3')
def test_switch_with_local_commits(self): """Test switch complains about local commits unless --force given.""" tree = self._setup_tree() to_branch = tree.bzrdir.sprout('branch-2').open_branch() self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') checkout = tree.branch.create_checkout('checkout') self.build_tree(['checkout/file-3']) checkout.add('file-3') checkout.commit(message='local only commit', local=True) self.build_tree(['checkout/file-4']) # Check the error reporting is as expected err = self.assertRaises(errors.BzrCommandError, switch.switch, checkout.bzrdir, to_branch) self.assertContainsRe(str(err), 'Cannot switch as local commits found in the checkout.') # Check all is ok when force is given self.failIfExists('checkout/file-1') self.failUnlessExists('checkout/file-2') switch.switch(checkout.bzrdir, to_branch, force=True) self.failUnlessExists('checkout/file-1') self.failIfExists('checkout/file-2') self.failIfExists('checkout/file-3') self.failUnlessExists('checkout/file-4') # Check that the checkout is a true mirror of the bound branch self.assertEqual(to_branch.last_revision_info(), checkout.branch.last_revision_info())
def test_switch_with_local_commits(self): """Test switch complains about local commits unless --force given.""" tree = self._setup_tree() to_branch = tree.bzrdir.sprout('branch-2').open_branch() self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') checkout = tree.branch.create_checkout('checkout') self.build_tree(['checkout/file-3']) checkout.add('file-3') checkout.commit(message='local only commit', local=True) self.build_tree(['checkout/file-4']) # Check the error reporting is as expected err = self.assertRaises(errors.BzrCommandError, switch.switch, checkout.bzrdir, to_branch) self.assertContainsRe(str(err), 'Cannot switch as local commits found in the checkout.') # Check all is ok when force is given self.assertPathDoesNotExist('checkout/file-1') self.assertPathExists('checkout/file-2') switch.switch(checkout.bzrdir, to_branch, force=True) self.assertPathExists('checkout/file-1') self.assertPathDoesNotExist('checkout/file-2') self.assertPathDoesNotExist('checkout/file-3') self.assertPathExists('checkout/file-4') # Check that the checkout is a true mirror of the bound branch self.assertEqual(to_branch.last_revision_info(), checkout.branch.last_revision_info())
def test_switch_restore_uncommitted_same_revision(self): """Test switch updates tree and restores uncommitted changes.""" checkout, to_branch = self._setup_uncommitted(same_revision=True) old_branch = self._master_if_present(checkout.branch) switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True) checkout = workingtree.WorkingTree.open('checkout') switch.switch(checkout.bzrdir, old_branch, store_uncommitted=True) self.assertPathExists('checkout/file-3')
def test_switch_store_uncommitted(self): """Test switch updates tree and stores uncommitted changes.""" checkout, to_branch = self._setup_uncommitted() self.assertPathDoesNotExist('checkout/file-1') self.assertPathExists('checkout/file-2') switch.switch(checkout.bzrdir, to_branch, store_uncommitted=True) self.assertPathExists('checkout/file-1') self.assertPathDoesNotExist('checkout/file-2') self.assertPathDoesNotExist('checkout/file-3')
def test_switch_changing_root_id(self): tree = self._setup_tree() tree2 = self.make_branch_and_tree('tree-2') tree2.set_root_id('custom-root-id') self.build_tree(['tree-2/file-2']) tree2.add(['file-2']) tree2.commit('rev1b') checkout = tree.branch.create_checkout('checkout', lightweight=self.lightweight) switch.switch(checkout.bzrdir, tree2.branch) self.assertEqual('custom-root-id', tree2.get_root_id())
def test_switch_with_revision(self): """Test switch when a revision is given.""" # Create a tree with 2 revisions tree = self.make_branch_and_tree('branch-1') self.build_tree(['branch-1/file-1']) tree.add('file-1') tree.commit(rev_id='rev1', message='rev1') self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.commit(rev_id='rev2', message='rev2') # Check it out and switch to revision 1 checkout = tree.branch.create_checkout('checkout', lightweight=self.lightweight) switch.switch(checkout.bzrdir, tree.branch, revision_id="rev1") self.assertPathExists('checkout/file-1') self.assertPathDoesNotExist('checkout/file-2')
def test_switch_configurable_file_merger(self): class DummyMerger(_mod_merge.ConfigurableFileMerger): name_prefix = 'file' _mod_merge.Merger.hooks.install_named_hook( 'merge_file_content', DummyMerger, 'test factory') foo = self.make_branch('foo') checkout = foo.create_checkout('checkout', lightweight=True) self.build_tree_contents([('checkout/file', 'a')]) checkout.add('file') checkout.commit('a') bar = foo.bzrdir.sprout('bar').open_workingtree() self.build_tree_contents([('bar/file', 'b')]) bar.commit('b') self.build_tree_contents([('checkout/file', 'c')]) switch.switch(checkout.bzrdir, bar.branch)
def test_switch_updates(self): """Test switch updates tree and keeps uncommitted changes.""" tree = self._setup_tree() to_branch = tree.bzrdir.sprout('branch-2').open_branch() self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') checkout = tree.branch.create_checkout('checkout', lightweight=self.lightweight) self.build_tree(['checkout/file-3']) checkout.add('file-3') self.failIfExists('checkout/file-1') self.failUnlessExists('checkout/file-2') switch.switch(checkout.bzrdir, to_branch) self.failUnlessExists('checkout/file-1') self.failIfExists('checkout/file-2') self.failUnlessExists('checkout/file-3')
def test_content_filtering_applied_on_switch(self): # Create a source branch with two revisions source, fileid_1, fileid_2, fileid_3, fileid_4 = \ self.create_cf_tree_with_two_revisions(txt_reader=None, txt_writer=None, dir='branch-a') if not source.supports_content_filtering(): return # Now patch in content filtering and branch from revision 1 self.patch_in_content_filter() self.run_bzr('branch -r1 branch-a branch-b') # Now create a lightweight checkout referring to branch-b self.run_bzr('checkout --lightweight branch-b checkout') self.assertFileEqual("fOO tXT", 'checkout/file1.txt') # Switch it to branch-b and check the tree is updated checkout_control_dir = ControlDir.open_containing('checkout')[0] switch(checkout_control_dir, source.branch) self.assertFileEqual("fOO rocks!", 'checkout/file1.txt') self.assertFileEqual("hELLO wORLD", 'checkout/file4.txt')
def test_post_switch_hook(self): from bzrlib import switch calls = [] _mod_branch.Branch.hooks.install_named_hook('post_switch', calls.append, None) tree = self.make_branch_and_tree('branch-1') self.build_tree(['branch-1/file-1']) tree.add('file-1') tree.commit('rev1') to_branch = tree.bzrdir.sprout('branch-2').open_branch() self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') checkout = tree.branch.create_checkout('checkout') self.assertLength(0, calls) switch.switch(checkout.bzrdir, to_branch) self.assertLength(1, calls) params = calls[0] self.assertIsInstance(params, _mod_branch.SwitchHookParams) self.assertTrue(hasattr(params, 'to_branch')) self.assertTrue(hasattr(params, 'revision_id'))
def test_post_switch_hook(self): from bzrlib import switch calls = [] _mod_branch.Branch.hooks.install_named_hook('post_switch', calls.append, None) tree = self.make_branch_and_tree('branch-1') self.build_tree(['branch-1/file-1']) tree.add('file-1') tree.commit('rev1') to_branch = tree.bzrdir.sprout('branch-2').open_branch() self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') checkout = tree.branch.create_checkout('checkout') self.assertLength(0, calls) switch.switch(checkout.bzrdir, to_branch) self.assertLength(1, calls) params = calls[0] self.assertIsInstance(params, _mod_branch.SwitchHookParams) self.assertTrue(hasattr(params, 'to_branch')) self.assertTrue(hasattr(params, 'revision_id'))
def test_switch_after_branch_moved(self): """Test switch after the branch is moved.""" tree = self._setup_tree() checkout = tree.branch.create_checkout('checkout', lightweight=self.lightweight) self.build_tree(['branch-1/file-2']) tree.add('file-2') tree.remove('file-1') tree.commit('rev2') self.build_tree(['checkout/file-3']) checkout.add('file-3') checkout_dir = checkout.bzrdir # rename the branch on disk, the checkout object is now invalid. os.rename('branch-1', 'branch-2') to_branch = branch.Branch.open('branch-2') # Check fails without --force err = self.assertRaises((errors.NotBranchError, errors.BoundBranchConnectionFailure), switch.switch, checkout.bzrdir, to_branch) switch.switch(checkout.bzrdir, to_branch, force=True) self.failIfExists('checkout/file-1') self.failUnlessExists('checkout/file-2') self.failUnlessExists('checkout/file-3')