Rev 4964: (andrew) Add --revision option to 'bzr switch'. (#184559) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri Jan 15 04:49:50 GMT 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 4964 [merge]
revision-id: pqm at pqm.ubuntu.com-20100115044948-yxz5m3vchxapbq22
parent: pqm at pqm.ubuntu.com-20100115032936-obafmsnn0d1ij450
parent: andrew.bennetts at canonical.com-20100115040645-sbjgbhytbw7zw3v3
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2010-01-15 04:49:48 +0000
message:
  (andrew) Add --revision option to 'bzr switch'. (#184559)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/switch.py               switch.py-20071116011000-v5lnw7d2wkng9eux-1
  bzrlib/tests/blackbox/test_switch.py test_switch.py-20071122111948-0c5en6uz92bwl76h-1
  bzrlib/tests/test_switch.py    test_switch.py-20071116011000-v5lnw7d2wkng9eux-2
=== modified file 'NEWS'
--- a/NEWS	2010-01-14 08:20:18 +0000
+++ b/NEWS	2010-01-15 04:06:45 +0000
@@ -23,6 +23,9 @@
 * ``bzr branch`` now takes a ``--bind`` option. This lets you
   branch and bind all in one command. (Ian Clatworthy)
 
+* ``bzr switch`` now takes a ``--revision`` option, to allow switching to
+  a specific revision of a branch. (Daniel Watkins, #183559)
+
 * ``bzr unshelve --preview`` can now be used to show how a patch on the
   shelf would be applied to the working tree.
   (Guilherme Salgado, #308122)

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2010-01-15 02:28:49 +0000
+++ b/bzrlib/builtins.py	2010-01-15 04:05:36 +0000
@@ -5437,18 +5437,26 @@
     that of the master.
     """
 
-    takes_args = ['to_location']
+    takes_args = ['to_location?']
     takes_options = [Option('force',
                         help='Switch even if local commits will be lost.'),
+                     'revision',
                      Option('create-branch', short_name='b',
                         help='Create the target branch from this one before'
                              ' switching to it.'),
-                     ]
+                    ]
 
-    def run(self, to_location, force=False, create_branch=False):
+    def run(self, to_location=None, force=False, create_branch=False,
+            revision=None):
         from bzrlib import switch
         tree_location = '.'
+        revision = _get_one_revision('switch', revision)
         control_dir = bzrdir.BzrDir.open_containing(tree_location)[0]
+        if to_location is None:
+            if revision is None:
+                raise errors.BzrCommandError('You must supply either a'
+                                             ' revision or a location')
+            to_location = '.'
         try:
             branch = control_dir.open_branch()
             had_explicit_nick = branch.get_config().has_explicit_nickname()
@@ -5468,13 +5476,6 @@
             to_branch = branch.bzrdir.sprout(to_location,
                                  possible_transports=[branch.bzrdir.root_transport],
                                  source_branch=branch).open_branch()
-            # try:
-            #     from_branch = control_dir.open_branch()
-            # except errors.NotBranchError:
-            #     raise BzrCommandError('Cannot create a branch from this'
-            #         ' location when we cannot open this branch')
-            # from_branch.bzrdir.sprout(
-            pass
         else:
             try:
                 to_branch = Branch.open(to_location)
@@ -5482,7 +5483,9 @@
                 this_url = self._get_branch_location(control_dir)
                 to_branch = Branch.open(
                     urlutils.join(this_url, '..', to_location))
-        switch.switch(control_dir, to_branch, force)
+        if revision is not None:
+            revision = revision.as_revision_id(to_branch)
+        switch.switch(control_dir, to_branch, force, revision_id=revision)
         if had_explicit_nick:
             branch = control_dir.open_branch() #get the new branch!
             branch.nick = to_branch.nick

=== modified file 'bzrlib/switch.py'
--- a/bzrlib/switch.py	2009-06-19 21:16:31 +0000
+++ b/bzrlib/switch.py	2010-01-12 03:53:21 +0000
@@ -22,12 +22,13 @@
 from bzrlib.trace import note
 
 
-def switch(control_dir, to_branch, force=False, quiet=False):
+def switch(control_dir, to_branch, force=False, quiet=False, revision_id=None):
     """Switch the branch associated with a checkout.
 
     :param control_dir: BzrDir of the checkout to change
     :param to_branch: branch that the checkout is to reference
     :param force: skip the check for local commits in a heavy checkout
+    :param revision_id: revision ID to switch to.
     """
     _check_pending_merges(control_dir, force)
     try:
@@ -36,7 +37,7 @@
         source_repository = to_branch.repository
     _set_branch_location(control_dir, to_branch, force)
     tree = control_dir.open_workingtree()
-    _update(tree, source_repository, quiet)
+    _update(tree, source_repository, quiet, revision_id)
 
 
 def _check_pending_merges(control, force=False):
@@ -118,7 +119,7 @@
     return False
 
 
-def _update(tree, source_repository, quiet=False):
+def _update(tree, source_repository, quiet=False, revision_id=None):
     """Update a working tree to the latest revision of its branch.
 
     :param tree: the working tree
@@ -127,12 +128,14 @@
     tree.lock_tree_write()
     try:
         to_branch = tree.branch
-        if tree.last_revision() == to_branch.last_revision():
+        if revision_id is None:
+            revision_id = to_branch.last_revision()
+        if tree.last_revision() == revision_id:
             if not quiet:
                 note("Tree is up to date at revision %d.", to_branch.revno())
             return
         base_tree = source_repository.revision_tree(tree.last_revision())
-        merge.Merge3Merger(tree, tree, base_tree, to_branch.basis_tree())
+        merge.Merge3Merger(tree, tree, base_tree, to_branch.repository.revision_tree(revision_id))
         tree.set_last_revision(to_branch.last_revision())
         if not quiet:
             note('Updated to revision %d.' % to_branch.revno())

=== modified file 'bzrlib/tests/blackbox/test_switch.py'
--- a/bzrlib/tests/blackbox/test_switch.py	2009-12-11 17:04:09 +0000
+++ b/bzrlib/tests/blackbox/test_switch.py	2010-01-12 03:53:21 +0000
@@ -27,6 +27,15 @@
 
 class TestSwitch(ExternalBase):
 
+    def _create_sample_tree(self):
+        tree = self.make_branch_and_tree('branch-1')
+        self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
+        tree.add('file-1')
+        tree.commit('rev1')
+        tree.add('file-2')
+        tree.commit('rev2')
+        return tree
+
     def test_switch_up_to_date_light_checkout(self):
         self.make_branch_and_tree('branch')
         self.run_bzr('branch branch branch2')
@@ -135,6 +144,26 @@
         self.assertEqual(branchb_id, checkout.last_revision())
         self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
 
+    def test_switch_revision(self):
+        tree = self._create_sample_tree()
+        checkout = tree.branch.create_checkout('checkout', lightweight=True)
+        self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
+        self.failUnlessExists('checkout/file-1')
+        self.failIfExists('checkout/file-2')
+
+    def test_switch_only_revision(self):
+        tree = self._create_sample_tree()
+        checkout = tree.branch.create_checkout('checkout', lightweight=True)
+        self.failUnlessExists('checkout/file-1')
+        self.failUnlessExists('checkout/file-2')
+        self.run_bzr(['switch', '-r1'], working_dir='checkout')
+        self.failUnlessExists('checkout/file-1')
+        self.failIfExists('checkout/file-2')
+        # Check that we don't accept a range
+        self.run_bzr_error(
+            ['bzr switch --revision takes exactly one revision identifier'],
+            ['switch', '-r0..2'], working_dir='checkout')
+
     def prepare_lightweight_switch(self):
         branch = self.make_branch('branch')
         branch.create_checkout('tree', lightweight=True)
@@ -196,4 +225,3 @@
         self.run_bzr('switch -b foo:branch2', working_dir='tree')
         tree = WorkingTree.open('tree')
         self.assertEndsWith(tree.branch.base, 'foo-branch2/')
-

=== modified file 'bzrlib/tests/test_switch.py'
--- a/bzrlib/tests/test_switch.py	2010-01-12 18:10:23 +0000
+++ b/bzrlib/tests/test_switch.py	2010-01-15 04:05:36 +0000
@@ -100,6 +100,23 @@
         self.assertContainsRe(str(err),
             "Pending merges must be committed or reverted before using switch")
 
+    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.failUnlessExists('checkout/file-1')
+        self.failIfExists('checkout/file-2')
+
     def test_switch_changing_root_id(self):
         tree = self._setup_tree()
         tree2 = self.make_branch_and_tree('tree-2')




More information about the bazaar-commits mailing list