Rev 4521: 'bzr switch -b' can now be used to create the branch while you switch to it. in http://bazaar.launchpad.net/~jameinel/bzr/1.18-switch-branch

John Arbash Meinel john at arbash-meinel.com
Thu Jul 9 16:19:02 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/1.18-switch-branch

------------------------------------------------------------
revno: 4521
revision-id: john at arbash-meinel.com-20090709151857-0halwp51u1on5p1n
parent: pqm at pqm.ubuntu.com-20090708193407-g5jilvy35c8g6z2o
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 1.18-switch-branch
timestamp: Thu 2009-07-09 10:18:57 -0500
message:
  'bzr switch -b' can now be used to create the branch while you switch to it.
  
  This is just a convenience thing, but it shortens:
    bzr branch . ../new-branch
    bzr switch ../new-branch
  into a single:
    bzr switch -b ../new-branch
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2009-07-08 18:05:38 +0000
+++ b/NEWS	2009-07-09 15:18:57 +0000
@@ -36,6 +36,11 @@
   behavior.
   (Vincent Ladeuil, #206577)
 
+* ``bzr switch --create-branch/-b`` can now be used to create and switch
+  to a new branch. Supplying a name without a ``/`` will create the branch
+  relative to the existing branch. (similar to how ``bzr switch name``
+  works when the branch already exists.) (John Arbash Meinel)
+
 
 Bug Fixes
 *********

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2009-07-02 11:37:38 +0000
+++ b/bzrlib/builtins.py	2009-07-09 15:18:57 +0000
@@ -5299,10 +5299,13 @@
 
     takes_args = ['to_location']
     takes_options = [Option('force',
-                        help='Switch even if local commits will be lost.')
+                        help='Switch even if local commits will be lost.'),
+                     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):
+    def run(self, to_location, force=False, create_branch=False):
         from bzrlib import switch
         tree_location = '.'
         control_dir = bzrdir.BzrDir.open_containing(tree_location)[0]
@@ -5310,13 +5313,33 @@
             branch = control_dir.open_branch()
             had_explicit_nick = branch.get_config().has_explicit_nickname()
         except errors.NotBranchError:
+            branch = None
             had_explicit_nick = False
-        try:
-            to_branch = Branch.open(to_location)
-        except errors.NotBranchError:
-            this_url = self._get_branch_location(control_dir)
-            to_branch = Branch.open(
-                urlutils.join(this_url, '..', to_location))
+        if create_branch:
+            if branch is None:
+                raise errors.BzrCommandError('cannot create branch without'
+                                             ' source branch')
+            if '/' not in to_location and '\\' not in to_location:
+                # This path is meant to be relative to the existing branch
+                this_url = self._get_branch_location(control_dir)
+                to_location = urlutils.join(this_url, '..', to_location)
+            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)
+            except errors.NotBranchError:
+                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 had_explicit_nick:
             branch = control_dir.open_branch() #get the new branch!

=== modified file 'bzrlib/tests/blackbox/test_switch.py'
--- a/bzrlib/tests/blackbox/test_switch.py	2009-06-10 03:56:49 +0000
+++ b/bzrlib/tests/blackbox/test_switch.py	2009-07-09 15:18:57 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2007 Canonical Ltd
+# Copyright (C) 2007, 2008, 2009 Canonical Ltd
 # -*- coding: utf-8 -*-
 #
 # This program is free software; you can redistribute it and/or modify
@@ -150,3 +150,36 @@
         self.run_bzr('switch --force branch1', working_dir='tree')
         branch_location = WorkingTree.open('tree').branch.base
         self.assertEndsWith(branch_location, 'branch1/')
+
+    def test_create_branch_no_branch(self):
+        self.prepare_lightweight_switch()
+        self.run_bzr_error(['cannot create branch without source branch'],
+            'switch --create-branch ../branch2', working_dir='tree')
+
+    def test_create_branch(self):
+        branch = self.make_branch('branch')
+        tree = branch.create_checkout('tree', lightweight=True)
+        tree.commit('one', rev_id='rev-1')
+        self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
+        tree = WorkingTree.open('tree')
+        self.assertEndsWith(tree.branch.base, '/branch2/')
+
+    def test_create_branch_local(self):
+        branch = self.make_branch('branch')
+        tree = branch.create_checkout('tree', lightweight=True)
+        tree.commit('one', rev_id='rev-1')
+        self.run_bzr('switch --create-branch branch2', working_dir='tree')
+        tree = WorkingTree.open('tree')
+        # The new branch should have been created at the same level as
+        # 'branch', because we did not have a '/' segment
+        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
+
+    def test_create_branch_short_name(self):
+        branch = self.make_branch('branch')
+        tree = branch.create_checkout('tree', lightweight=True)
+        tree.commit('one', rev_id='rev-1')
+        self.run_bzr('switch -b branch2', working_dir='tree')
+        tree = WorkingTree.open('tree')
+        # The new branch should have been created at the same level as
+        # 'branch', because we did not have a '/' segment
+        self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)



More information about the bazaar-commits mailing list