Rev 6443: (jelmer) Factor out finding of sibling branches from 'bzr switch'. (Jelmer in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

Patch Queue Manager pqm at pqm.ubuntu.com
Fri Jan 20 15:48:39 UTC 2012


At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 6443 [merge]
revision-id: pqm at pqm.ubuntu.com-20120120154839-y6yh1m1dzknmub8f
parent: pqm at pqm.ubuntu.com-20120119131539-19c0a9ad8lpnvs95
parent: jelmer at samba.org-20120107030352-cs2ir5wh1f6bkjfk
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2012-01-20 15:48:39 +0000
message:
  (jelmer) Factor out finding of sibling branches from 'bzr switch'. (Jelmer
   Vernooij)
modified:
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2012-01-19 12:01:12 +0000
+++ b/bzrlib/builtins.py	2012-01-20 15:48:39 +0000
@@ -83,6 +83,74 @@
     )
 
 
+def _get_branch_location(control_dir):
+    """Return location of branch for this control dir."""
+    try:
+        this_branch = control_dir.open_branch()
+        # This may be a heavy checkout, where we want the master branch
+        master_location = this_branch.get_bound_location()
+        if master_location is not None:
+            return master_location
+        # If not, use a local sibling
+        return this_branch.base
+    except errors.NotBranchError:
+        format = control_dir.find_branch_format()
+        if getattr(format, 'get_reference', None) is not None:
+            return format.get_reference(control_dir)
+        else:
+            return control_dir.root_transport.base
+
+
+def lookup_new_sibling_branch(control_dir, location):
+    """Lookup the location for a new sibling branch.
+
+    :param control_dir: Control directory relative to which to look up
+        the name.
+    :param location: Name of the new branch
+    :return: Full location to the new branch
+    """
+    location = directory_service.directories.dereference(location)
+    if '/' not in location and '\\' not in location:
+        # This path is meant to be relative to the existing branch
+        this_url = _get_branch_location(control_dir)
+        # Perhaps the target control dir supports colocated branches?
+        try:
+            root = controldir.ControlDir.open(this_url,
+                possible_transports=[control_dir.user_transport])
+        except errors.NotBranchError:
+            colocated = False
+        else:
+            colocated = root._format.colocated_branches
+
+        if colocated:
+            return urlutils.join_segment_parameters(this_url,
+                {"branch": urlutils.escape(location)})
+        else:
+            return urlutils.join(this_url, '..', urlutils.escape(location))
+    return location
+
+
+def lookup_sibling_branch(control_dir, location):
+    """Lookup sibling branch.
+    
+    :param control_dir: Control directory relative to which to lookup the
+        location.
+    :param location: Location to look up
+    :return: branch to open
+    """
+    try:
+        # Perhaps it's a colocated branch?
+        return control_dir.open_branch(location)
+    except (errors.NotBranchError, errors.NoColocatedBranchSupport):
+        try:
+            return Branch.open(location)
+        except errors.NotBranchError:
+            this_url = _get_branch_location(control_dir)
+            return Branch.open(
+                urlutils.join(
+                    this_url, '..', urlutils.escape(location)))
+
+
 @symbol_versioning.deprecated_function(symbol_versioning.deprecated_in((2, 3, 0)))
 def tree_files(file_list, default_branch=u'.', canonicalize=True,
     apply_view=True):
@@ -6147,42 +6215,14 @@
             had_explicit_nick = False
         if create_branch:
             if branch is None:
-                raise errors.BzrCommandError(gettext('cannot create branch without'
-                                             ' source branch'))
-            to_location = directory_service.directories.dereference(
-                              to_location)
-            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)
-                # Perhaps the target control dir supports colocated branches?
-                try:
-                    root = controldir.ControlDir.open(this_url,
-                        possible_transports=[control_dir.user_transport])
-                except errors.NotBranchError:
-                    colocated = False
-                else:
-                    colocated = root._format.colocated_branches
-                if colocated:
-                    to_location = urlutils.join_segment_parameters(this_url,
-                        {"branch": urlutils.escape(to_location)})
-                else:
-                    to_location = urlutils.join(
-                        this_url, '..', urlutils.escape(to_location))
+                raise errors.BzrCommandError(
+                    gettext('cannot create branch without source branch'))
+            to_location = lookup_new_sibling_branch(control_dir, to_location)
             to_branch = branch.bzrdir.sprout(to_location,
-                                 possible_transports=[branch.bzrdir.root_transport],
-                                 source_branch=branch).open_branch()
+                 possible_transports=[branch.bzrdir.root_transport],
+                 source_branch=branch).open_branch()
         else:
-            # Perhaps it's a colocated branch?
-            try:
-                to_branch = control_dir.open_branch(to_location)
-            except (errors.NotBranchError, errors.NoColocatedBranchSupport):
-                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, '..', urlutils.escape(to_location)))
+            to_branch = lookup_sibling_branch(control_dir, to_location)
         if revision is not None:
             revision = revision.as_revision_id(to_branch)
         switch.switch(control_dir, to_branch, force, revision_id=revision)
@@ -6192,22 +6232,6 @@
         note(gettext('Switched to branch: %s'),
             urlutils.unescape_for_display(to_branch.base, 'utf-8'))
 
-    def _get_branch_location(self, control_dir):
-        """Return location of branch for this control dir."""
-        try:
-            this_branch = control_dir.open_branch()
-            # This may be a heavy checkout, where we want the master branch
-            master_location = this_branch.get_bound_location()
-            if master_location is not None:
-                return master_location
-            # If not, use a local sibling
-            return this_branch.base
-        except errors.NotBranchError:
-            format = control_dir.find_branch_format()
-            if getattr(format, 'get_reference', None) is not None:
-                return format.get_reference(control_dir)
-            else:
-                return control_dir.root_transport.base
 
 
 class cmd_view(Command):




More information about the bazaar-commits mailing list