Rev 4295: Move directory checking for bzr push options into Branch.create_clone_on_transport. in http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

Robert Collins robertc at robertcollins.net
Mon Apr 20 05:20:22 BST 2009


At http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

------------------------------------------------------------
revno: 4295
revision-id: robertc at robertcollins.net-20090420041945-qvim67wg99c3euki
parent: pqm at pqm.ubuntu.com-20090415172601-buxpv8kc1qf9uork
committer: Robert Collins <robertc at robertcollins.net>
branch nick: push.roundtrips
timestamp: Mon 2009-04-20 14:19:45 +1000
message:
  Move directory checking for bzr push options into Branch.create_clone_on_transport.
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2009-04-15 07:30:34 +0000
+++ b/bzrlib/branch.py	2009-04-20 04:19:45 +0000
@@ -1134,16 +1134,57 @@
         return format
 
     def create_clone_on_transport(self, to_transport, revision_id=None,
-        stacked_on=None):
+        stacked_on=None, create_prefix=False, use_existing_dir=False):
         """Create a clone of this branch and its bzrdir.
 
         :param to_transport: The transport to clone onto.
         :param revision_id: The revision id to use as tip in the new branch.
             If None the tip is obtained from this branch.
         :param stacked_on: An optional URL to stack the clone on.
+        :param create_prefix: Create any missing directories leading up to
+            to_transport.
+        :param use_existing_dir: Use an existing directory if one exists.
         """
+        # The destination doesn't exist; create it.
+        # XXX: Refactor the create_prefix/no_create_prefix code into a
+        #      common helper function
+
+        def make_directory(transport):
+            transport.mkdir('.')
+            return transport
+
+        def redirected(transport, e, redirection_notice):
+            note(redirection_notice)
+            return transport._redirected_to(e.source, e.target)
+
+        try:
+            to_transport = transport.do_catching_redirections(
+                make_directory, to_transport, redirected)
+        except errors.FileExists:
+            if not use_existing_dir:
+                raise errors.BzrCommandError("Target directory %s"
+                     " already exists, but does not have a valid .bzr"
+                     " directory. Supply --use-existing-dir to push"
+                     " there anyway." % to_transport.base)
+        except errors.NoSuchFile:
+            if not create_prefix:
+                raise errors.BzrCommandError("Parent directory of %s"
+                    " does not exist."
+                    "\nYou may supply --create-prefix to create all"
+                    " leading parent directories."
+                    % to_transport.base)
+            to_transport.create_prefix()
+        except errors.TooManyRedirections:
+            raise errors.BzrCommandError("Too many redirections trying "
+                                         "to make %s." % to_transport.base)
+
+        # Now the target directory exists, but doesn't have a .bzr
+        # directory. So we need to create it, along with any work to create
+        # all of the dependent branches, etc.
         # XXX: Fix the bzrdir API to allow getting the branch back from the
         # clone call. Or something. 20090224 RBC/spiv.
+        if revision_id is None:
+            revision_id = self.last_revision()
         dir_to = self.bzrdir.clone_on_transport(to_transport,
             revision_id=revision_id, stacked_on=stacked_on)
         return dir_to.open_branch()

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2009-04-09 23:12:55 +0000
+++ b/bzrlib/builtins.py	2009-04-20 04:19:45 +0000
@@ -1017,7 +1017,7 @@
         if revision is not None:
             revision_id = revision.in_history(br_from).rev_id
         else:
-            revision_id = br_from.last_revision()
+            revision_id = None
 
         # Get the stacked_on branch, if any
         if stacked_on is not None:
@@ -1575,7 +1575,7 @@
                     "\nYou may supply --create-prefix to create all"
                     " leading parent directories."
                     % location)
-            _create_prefix(to_transport)
+            to_transport.create_prefix()
 
         try:
             a_bzrdir = bzrdir.BzrDir.open_from_transport(to_transport)
@@ -5573,28 +5573,6 @@
                    dry_run=dry_run, no_prompt=force)
 
 
-def _create_prefix(cur_transport):
-    needed = [cur_transport]
-    # Recurse upwards until we can create a directory successfully
-    while True:
-        new_transport = cur_transport.clone('..')
-        if new_transport.base == cur_transport.base:
-            raise errors.BzrCommandError(
-                "Failed to create path prefix for %s."
-                % cur_transport.base)
-        try:
-            new_transport.mkdir('.')
-        except errors.NoSuchFile:
-            needed.append(new_transport)
-            cur_transport = new_transport
-        else:
-            break
-    # Now we only need to create child directories
-    while needed:
-        cur_transport = needed.pop()
-        cur_transport.ensure_base()
-
-
 # these get imported and then picked up by the scan for cmd_*
 # TODO: Some more consistent way to split command definitions across files;
 # we do need to load at least some information about them to know of

=== modified file 'bzrlib/push.py'
--- a/bzrlib/push.py	2009-04-03 00:02:29 +0000
+++ b/bzrlib/push.py	2009-04-20 04:19:45 +0000
@@ -79,52 +79,17 @@
         directory exists without a current .bzr directory in it
     """
     to_transport = transport.get_transport(location)
-    br_to = repository_to = dir_to = None
     try:
         dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
     except errors.NotBranchError:
-        pass # Didn't find anything
+        # Didn't find anything
+        dir_to = None
 
-    push_result = PushResult()
     if dir_to is None:
-        # The destination doesn't exist; create it.
-        # XXX: Refactor the create_prefix/no_create_prefix code into a
-        #      common helper function
-
-        def make_directory(transport):
-            transport.mkdir('.')
-            return transport
-
-        def redirected(transport, e, redirection_notice):
-            note(redirection_notice)
-            return transport._redirected_to(e.source, e.target)
-
-        try:
-            to_transport = transport.do_catching_redirections(
-                make_directory, to_transport, redirected)
-        except errors.FileExists:
-            if not use_existing_dir:
-                raise errors.BzrCommandError("Target directory %s"
-                     " already exists, but does not have a valid .bzr"
-                     " directory. Supply --use-existing-dir to push"
-                     " there anyway." % location)
-        except errors.NoSuchFile:
-            if not create_prefix:
-                raise errors.BzrCommandError("Parent directory of %s"
-                    " does not exist."
-                    "\nYou may supply --create-prefix to create all"
-                    " leading parent directories."
-                    % location)
-            builtins._create_prefix(to_transport)
-        except errors.TooManyRedirections:
-            raise errors.BzrCommandError("Too many redirections trying "
-                                         "to make %s." % location)
-
-        # Now the target directory exists, but doesn't have a .bzr
-        # directory. So we need to create it, along with any work to create
-        # all of the dependent branches, etc.
         br_to = br_from.create_clone_on_transport(to_transport,
-            revision_id=revision_id, stacked_on=stacked_on)
+            revision_id=revision_id, stacked_on=stacked_on,
+            create_prefix=create_prefix, use_existing_dir=use_existing_dir)
+        push_result = PushResult()
         # TODO: Some more useful message about what was copied
         try:
             push_result.stacked_on = br_to.get_stacked_on_url()

=== modified file 'bzrlib/smart/request.py'
--- a/bzrlib/smart/request.py	2009-04-15 02:07:35 +0000
+++ b/bzrlib/smart/request.py	2009-04-20 04:19:45 +0000
@@ -176,6 +176,8 @@
             return client_path
         if not client_path.startswith('/'):
             client_path = '/' + client_path
+        if client_path + '/' == self._root_client_path:
+            return '.'
         if client_path.startswith(self._root_client_path):
             path = client_path[len(self._root_client_path):]
             relpath = urlutils.joinpath('/', path)

=== modified file 'bzrlib/tests/branch_implementations/test_create_clone.py'
--- a/bzrlib/tests/branch_implementations/test_create_clone.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/tests/branch_implementations/test_create_clone.py	2009-04-20 04:19:45 +0000
@@ -25,6 +25,46 @@
 
 class TestCreateClone(TestCaseWithBranch):
 
+    def test_create_clone_on_transport_missing_parent_dir(self):
+        tree = self.make_branch_and_tree('source')
+        tree.commit('a commit')
+        source = tree.branch
+        target_transport = self.get_transport('subdir').clone('target')
+        self.assertRaises(errors.BzrCommandError,
+            tree.branch.create_clone_on_transport, target_transport)
+        self.assertFalse(self.get_transport('.').has('subdir'))
+
+    def test_create_clone_on_transport_missing_parent_dir_create(self):
+        tree = self.make_branch_and_tree('source')
+        tree.commit('a commit')
+        source = tree.branch
+        target_transport = self.get_transport('subdir').clone('target')
+        result = tree.branch.create_clone_on_transport(target_transport,
+            create_prefix=True)
+        self.assertEqual(source.last_revision(), result.last_revision())
+        self.assertEqual(target_transport.base,
+            result.bzrdir.root_transport.base)
+
+    def test_create_clone_on_transport_use_existing_dir_false(self):
+        tree = self.make_branch_and_tree('source')
+        tree.commit('a commit')
+        source = tree.branch
+        target_transport = self.get_transport('target')
+        target_transport.create_prefix()
+        self.assertRaises(errors.BzrCommandError,
+            tree.branch.create_clone_on_transport, target_transport)
+        self.assertFalse(target_transport.has(".bzr"))
+
+    def test_create_clone_on_transport_use_existing_dir_true(self):
+        tree = self.make_branch_and_tree('source')
+        tree.commit('a commit')
+        source = tree.branch
+        target_transport = self.get_transport('target')
+        target_transport.create_prefix()
+        result = tree.branch.create_clone_on_transport(target_transport,
+            use_existing_dir=True)
+        self.assertEqual(source.last_revision(), result.last_revision())
+
     def test_create_clone_on_transport_no_revision_id(self):
         tree = self.make_branch_and_tree('source')
         tree.commit('a commit')

=== modified file 'bzrlib/tests/test_transport_implementations.py'
--- a/bzrlib/tests/test_transport_implementations.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/tests/test_transport_implementations.py	2009-04-20 04:19:45 +0000
@@ -674,6 +674,16 @@
             for f in files:
                 self.assertTransportMode(temp_transport, f, mode)
 
+    def test_create_prefix(self):
+        t = self.get_transport()
+        sub = t.clone('foo').clone('bar')
+        try:
+            sub.create_prefix()
+        except TransportNotPossible:
+            self.assertTrue(t.is_readonly())
+        else:
+            self.assertTrue(t.has('foo/bar'))
+
     def test_append_file(self):
         t = self.get_transport()
 

=== modified file 'bzrlib/transport/__init__.py'
--- a/bzrlib/transport/__init__.py	2009-04-04 01:45:09 +0000
+++ b/bzrlib/transport/__init__.py	2009-04-20 04:19:45 +0000
@@ -328,6 +328,31 @@
         """
         raise NotImplementedError(self.clone)
 
+    def create_prefix(self):
+        """Create all the directorie sleading down to self.base."""
+        cur_transport = self
+        needed = [cur_transport]
+        # Recurse upwards until we can create a directory successfully
+        while True:
+            new_transport = cur_transport.clone('..')
+            if new_transport.base == cur_transport.base:
+                raise errors.BzrCommandError(
+                    "Failed to create path prefix for %s."
+                    % cur_transport.base)
+            try:
+                new_transport.mkdir('.')
+            except errors.NoSuchFile:
+                needed.append(new_transport)
+                cur_transport = new_transport
+            except errors.FileExists:
+                break
+            else:
+                break
+        # Now we only need to create child directories
+        while needed:
+            cur_transport = needed.pop()
+            cur_transport.ensure_base()
+
     def ensure_base(self):
         """Ensure that the directory this transport references exists.
 




More information about the bazaar-commits mailing list