Rev 6519: (jelmer) Add Tree.iter_child_entries(). (Jelmer Vernooij) in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

Patch Queue Manager pqm at pqm.ubuntu.com
Mon Apr 2 02:36:46 UTC 2012


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

------------------------------------------------------------
revno: 6519 [merge]
revision-id: pqm at pqm.ubuntu.com-20120402023645-28dkcb0awh3i1flc
parent: pqm at pqm.ubuntu.com-20120402002446-ib9uf9n722nxnajz
parent: jelmer at samba.org-20120402014426-0o5qtysohyl006b2
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2012-04-02 02:36:45 +0000
message:
  (jelmer) Add Tree.iter_child_entries(). (Jelmer Vernooij)
removed:
  bzrlib/tests/per_branch/test_bound_sftp.py.THIS test_bound_sftp.py-20051231055311-2f96048c4f0940ef
modified:
  bzrlib/add.py                  add.py-20050323030017-3a77d63feda58e33
  bzrlib/bundle/serializer/v08.py v06.py-20051119041339-ee43f97270b01823
  bzrlib/tests/per_interrepository/test_fetch.py test_fetch.py-20080425213627-j60cjh782ufm83ry-1
  bzrlib/tests/per_tree/test_tree.py test_tree.py-20061215160206-usu7lwcj8aq2n3br-1
  bzrlib/transform.py            transform.py-20060105172343-dd99e54394d91687
  bzrlib/tree.py                 tree.py-20050309040759-9d5f2496be663e77
=== modified file 'bzrlib/add.py'
--- a/bzrlib/add.py	2012-02-06 23:38:33 +0000
+++ b/bzrlib/add.py	2012-02-21 12:53:02 +0000
@@ -127,15 +127,13 @@
         we look for a file with the same name in that directory.
         Else, we look for an entry in the base tree with the same path.
         """
-
         if self.base_tree.has_id(parent_ie.file_id):
-            # FIXME: Handle nested trees
-            base_parent_ie = self.base_tree.root_inventory[parent_ie.file_id]
-            base_child_ie = base_parent_ie.children.get(
+            base_path = osutils.pathjoin(
+                self.base_tree.id2path(parent_ie.file_id),
                 osutils.basename(path))
-            if base_child_ie is not None:
-                return (base_child_ie.file_id,
-                        self.base_tree.id2path(base_child_ie.file_id))
+            base_id = self.base_tree.path2id(base_path)
+            if base_id is not None:
+                return (base_id, base_path)
         full_base_path = osutils.pathjoin(self.base_path, path)
         # This may return None, but it is our last attempt
         return self.base_tree.path2id(full_base_path), full_base_path

=== modified file 'bzrlib/bundle/serializer/v08.py'
--- a/bzrlib/bundle/serializer/v08.py	2012-02-20 14:15:25 +0000
+++ b/bzrlib/bundle/serializer/v08.py	2012-04-01 23:39:57 +0000
@@ -287,15 +287,13 @@
 
         def finish_action(action, file_id, kind, meta_modified, text_modified,
                           old_path, new_path):
-            revision = new_tree.get_file_revision(file_id)
-            if revision != default_revision_id:
-                action.add_utf8_property('last-changed', revision)
+            entry = new_tree.root_inventory[file_id]
+            if entry.revision != default_revision_id:
+                action.add_utf8_property('last-changed', entry.revision)
             if meta_modified:
-                action.add_bool_property('executable',
-                    new_tree.is_executable(file_id))
+                action.add_bool_property('executable', entry.executable)
             if text_modified and kind == "symlink":
-                action.add_property('target',
-                    new_tree.get_symlink_target(file_id))
+                action.add_property('target', entry.symlink_target)
             if text_modified and kind == "file":
                 do_diff(file_id, old_path, new_path, action, force_binary)
             else:

=== removed file 'bzrlib/tests/per_branch/test_bound_sftp.py.THIS'
--- a/bzrlib/tests/per_branch/test_bound_sftp.py.THIS	2012-03-14 15:26:01 +0000
+++ b/bzrlib/tests/per_branch/test_bound_sftp.py.THIS	1970-01-01 00:00:00 +0000
@@ -1,331 +0,0 @@
-# Copyright (C) 2005, 2006, 2007, 2009, 2010 Robey Pointer <robey at lag.net>, Canonical Ltd
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-"""Tests for branches bound to an sftp branch."""
-
-
-import os
-
-from bzrlib import (
-    branch,
-    controldir,
-    errors,
-    tests,
-    )
-from bzrlib.tests import test_server
-from bzrlib.tests.matchers import RevisionHistoryMatches
-from bzrlib.transport import memory
-
-
-class BoundSFTPBranch(tests.TestCaseWithTransport):
-
-    def setUp(self):
-        tests.TestCaseWithTransport.setUp(self)
-        self.vfs_transport_factory = memory.MemoryServer
-        if self.transport_server is test_server.LocalURLServer:
-            self.transport_server = None
-
-    def create_branches(self):
-        self.build_tree(['base/', 'base/a', 'base/b'])
-        format = controldir.format_registry.make_bzrdir('knit')
-        try:
-            wt_base = controldir.ControlDir.create_standalone_workingtree(
-                self.get_url('base'), format=format)
-        except errors.NotLocalUrl:
-            raise tests.TestSkipped('Not a local URL')
-
-        b_base = wt_base.branch
-
-        wt_base.add('a')
-        wt_base.add('b')
-        wt_base.commit('first', rev_id='r at b-1')
-
-        wt_child = b_base.bzrdir.sprout('child').open_workingtree()
-        self.sftp_base = branch.Branch.open(self.get_url('base'))
-        wt_child.branch.bind(self.sftp_base)
-        # check the branch histories are ready for using in tests.
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-        return b_base, wt_child
-
-    def test_simple_binding(self):
-        self.build_tree(['base/', 'base/a', 'base/b', 'child/'])
-        try:
-            wt_base = controldir.ControlDir.create_standalone_workingtree(
-                self.get_url('base'))
-        except errors.NotLocalUrl:
-            raise tests.TestSkipped('Not a local URL')
-
-        wt_base.add('a')
-        wt_base.add('b')
-        wt_base.commit('first', rev_id='r at b-1')
-
-        b_base = wt_base.branch
-        # manually make a branch we can bind, because the default format
-        # may not be bindable-from, and we want to test the side effects etc
-        # of bondage.
-        format = controldir.format_registry.make_bzrdir('knit')
-        b_child = controldir.ControlDir.create_branch_convenience(
-            'child', format=format)
-        self.assertEqual(None, b_child.get_bound_location())
-        self.assertEqual(None, b_child.get_master_branch())
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-        b_child.bind(sftp_b_base)
-        self.assertEqual(sftp_b_base.base, b_child.get_bound_location())
-        # the bind must not have given b_child history:
-        self.assertThat(RevisionHistoryMatches([]), b_child)
-        # we should be able to update the branch at this point:
-        self.assertEqual(None, b_child.update())
-        # and now there must be history.
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_child)
-        # this line is more of a working tree test line, but - what the hey,
-        # it has work to do.
-        b_child.bzrdir.open_workingtree().update()
-        self.assertPathExists('child/a')
-        self.assertPathExists('child/b')
-
-        b_child.unbind()
-        self.assertEqual(None, b_child.get_bound_location())
-
-    def test_bound_commit(self):
-        b_base, wt_child = self.create_branches()
-
-        with open('child/a', 'wb') as f: f.write('new contents\n')
-        wt_child.commit('modified a', rev_id='r at c-2')
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2']), wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2']), b_base)
-
-    def test_bound_commit_fails_when_out_of_date(self):
-        # Make sure commit fails if out of date.
-        b_base, wt_child = self.create_branches()
-
-        with open('base/a', 'wb') as f: f.write('new base contents\n')
-        b_base.bzrdir.open_workingtree().commit('base', rev_id='r at b-2')
-
-        with open('child/b', 'wb') as f: f.write('new b child contents\n')
-        self.assertRaises(errors.BoundBranchOutOfDate,
-                wt_child.commit, 'child', rev_id='r at c-2')
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-
-        # This is all that cmd_update does
-        wt_child.pull(sftp_b_base, overwrite=False)
-
-        wt_child.commit('child', rev_id='r at c-3')
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2', 'r at c-3']),
-                wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2', 'r at c-3']),
-                b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2', 'r at c-3']),
-                sftp_b_base)
-
-    def test_double_binding(self):
-        b_base, wt_child = self.create_branches()
-
-        wt_child2 = wt_child.branch.create_checkout('child2')
-
-        with open('child2/a', 'wb') as f: f.write('new contents\n')
-        self.assertRaises(errors.CommitToDoubleBoundBranch,
-                wt_child2.commit, 'child2', rev_id='r at d-2')
-
-    def test_unbinding(self):
-        b_base, wt_child = self.create_branches()
-
-        # TestCaseWithSFTPServer only allows you to connect one time
-        # to the SFTP server. So we have to create a connection and
-        # keep it around, so that it can be reused
-        __unused_t = self.get_transport()
-
-        wt_base = b_base.bzrdir.open_workingtree()
-        with open('base/a', 'wb') as f: f.write('new base contents\n')
-        wt_base.commit('base', rev_id='r at b-2')
-
-        with open('child/b', 'wb') as f: f.write('new b child contents\n')
-        self.assertRaises(errors.BoundBranchOutOfDate,
-                wt_child.commit, 'child', rev_id='r at c-2')
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-        wt_child.branch.unbind()
-        wt_child.commit('child', rev_id='r at c-2')
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2']), wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2']), b_base)
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-        self.assertRaises(errors.DivergedBranches,
-                wt_child.branch.bind, sftp_b_base)
-
-    def test_commit_remote_bound(self):
-        # Make sure it is detected if the current base is bound during the
-        # objects lifetime, when the child goes to commit.
-        b_base, wt_child = self.create_branches()
-
-        b_base.bzrdir.sprout('newbase')
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-        sftp_b_newbase = branch.Branch.open(self.get_url('newbase'))
-
-        sftp_b_base.bind(sftp_b_newbase)
-
-        with open('child/a', 'wb') as f: f.write('new contents\n')
-        self.assertRaises(errors.CommitToDoubleBoundBranch,
-                wt_child.commit, 'failure', rev_id='r at c-2')
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), sftp_b_newbase)
-
-    def test_bind_diverged(self):
-        b_base, wt_child = self.create_branches()
-
-        wt_child.branch.unbind()
-        with open('child/a', 'ab') as f: f.write('child contents\n')
-        wt_child_rev = wt_child.commit('child', rev_id='r at c-2')
-
-        self.assertEqual(RevisionHistoryMatches(['r at b-1', 'r at c-2']),
-            wt_child.branch)
-        self.assertEqual(RevisionHistoryMatches(['r at b-1']), b_base)
-
-        with open('base/b', 'ab') as f: f.write('base contents\n')
-        b_base.bzrdir.open_workingtree().commit('base', rev_id='r at b-2')
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2']), b_base)
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-
-        self.assertRaises(errors.DivergedBranches,
-                wt_child.branch.bind, sftp_b_base)
-
-        wt_child.merge_from_branch(sftp_b_base)
-        self.assertEqual([wt_child_rev, 'r at b-2'], wt_child.get_parent_ids())
-        wt_child.commit('merged', rev_id='r at c-3')
-
-        # After a merge, trying to bind again should succeed but not push the
-        # new change.
-        wt_child.branch.bind(sftp_b_base)
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2']), b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2', 'r at c-3']),
-            wt_child.branch)
-
-    def test_bind_parent_ahead_preserves_parent(self):
-        b_base, wt_child = self.create_branches()
-
-        wt_child.branch.unbind()
-
-        with open('a', 'ab') as f: f.write('base changes\n')
-        wt_base = b_base.bzrdir.open_workingtree()
-        wt_base.commit('base', rev_id='r at b-2')
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at b-2']), b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-        wt_child.branch.bind(sftp_b_base)
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-
-        wt_child.branch.unbind()
-
-        # Check and make sure it also works if parent is ahead multiple
-        wt_base.commit('base 3', rev_id='r at b-3', allow_pointless=True)
-        wt_base.commit('base 4', rev_id='r at b-4', allow_pointless=True)
-        wt_base.commit('base 5', rev_id='r at b-5', allow_pointless=True)
-
-        self.assertThat(
-            RevisionHistoryMatches(['r at b-1', 'r at b-2', 'r at b-3', 'r at b-4', 'r at b-5']),
-            b_base)
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-
-        wt_child.branch.bind(sftp_b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), wt_child.branch)
-
-    def test_bind_child_ahead_preserves_child(self):
-        b_base, wt_child = self.create_branches()
-
-        wt_child.branch.unbind()
-
-        wt_child.commit('child', rev_id='r at c-2', allow_pointless=True)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2']), wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_base)
-
-        sftp_b_base = branch.Branch.open(self.get_url('base'))
-        wt_child.branch.bind(sftp_b_base)
-
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_base)
-
-        # Check and make sure it also works if child is ahead multiple
-        wt_child.branch.unbind()
-        wt_child.commit('child 3', rev_id='r at c-3', allow_pointless=True)
-        wt_child.commit('child 4', rev_id='r at c-4', allow_pointless=True)
-        wt_child.commit('child 5', rev_id='r at c-5', allow_pointless=True)
-
-        self.assertThat(
-            RevisionHistoryMatches(['r at b-1', 'r at c-2', 'r at c-3', 'r at c-4', 'r at c-5']),
-            wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_base)
-
-        wt_child.branch.bind(sftp_b_base)
-        self.assertThat(RevisionHistoryMatches(['r at b-1']), b_base)
-
-    def test_commit_after_merge(self):
-        b_base, wt_child = self.create_branches()
-
-        # We want merge to be able to be a local only
-        # operation, because it does not alter the branch data.
-
-        # But we can't fail afterwards
-
-        wt_other = wt_child.bzrdir.sprout('other').open_workingtree()
-
-        with open('other/c', 'wb') as f: f.write('file c\n')
-        wt_other.add('c')
-        wt_other.commit('adding c', rev_id='r at d-2')
-
-        self.assertFalse(wt_child.branch.repository.has_revision('r at d-2'))
-        self.assertFalse(b_base.repository.has_revision('r at d-2'))
-
-        wt_child.merge_from_branch(wt_other.branch)
-
-        self.assertPathExists('child/c')
-        self.assertEqual(['r at d-2'], wt_child.get_parent_ids()[1:])
-        self.assertTrue(wt_child.branch.repository.has_revision('r at d-2'))
-        self.assertFalse(b_base.repository.has_revision('r at d-2'))
-
-        # Commit should succeed, and cause merged revisions to
-        # be pushed into base
-        wt_child.commit('merge other', rev_id='r at c-2')
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2']), wt_child.branch)
-        self.assertThat(RevisionHistoryMatches(['r at b-1', 'r at c-2']), b_base)
-        self.assertTrue(b_base.repository.has_revision('r at d-2'))
-
-    def test_commit_fails(self):
-        b_base, wt_child = self.create_branches()
-
-        with open('a', 'ab') as f: f.write('child adds some text\n')
-
-        # this deletes the branch from memory
-        del b_base
-        # and this moves it out of the way on disk
-        os.rename('base', 'hidden_base')
-
-        self.assertRaises(errors.BoundBranchConnectionFailure,
-                wt_child.commit, 'added text', rev_id='r at c-2')
-
-    # TODO: jam 20051231 We need invasive failure tests, so that we can show
-    #       performance even when something fails.
-
-

=== modified file 'bzrlib/tests/per_interrepository/test_fetch.py'
--- a/bzrlib/tests/per_interrepository/test_fetch.py	2012-02-20 14:15:25 +0000
+++ b/bzrlib/tests/per_interrepository/test_fetch.py	2012-02-21 13:45:37 +0000
@@ -298,8 +298,7 @@
         stacked_branch.lock_read()
         self.addCleanup(stacked_branch.unlock)
         stacked_second_tree = stacked_branch.repository.revision_tree('second')
-        self.assertEqual(second_tree.root_inventory,
-            stacked_second_tree.root_inventory)
+        self.assertEqual(second_tree, stacked_second_tree)
         # Finally, it's not enough to see that the basis inventories are
         # present.  The texts introduced in merge (and only those) should be
         # present, and also generating a stream should succeed without blowing

=== modified file 'bzrlib/tests/per_tree/test_tree.py'
--- a/bzrlib/tests/per_tree/test_tree.py	2012-01-30 14:18:22 +0000
+++ b/bzrlib/tests/per_tree/test_tree.py	2012-02-21 13:35:17 +0000
@@ -249,6 +249,29 @@
                          output_order)
 
 
+class TestIterChildEntries(TestCaseWithTree):
+
+    def test_iteration_order(self):
+        work_tree = self.make_branch_and_tree('.')
+        self.build_tree(['a/', 'a/b/', 'a/b/c', 'a/d/', 'a/d/e', 'f/', 'f/g'])
+        work_tree.add(['a', 'a/b', 'a/b/c', 'a/d', 'a/d/e', 'f', 'f/g'])
+        tree = self._convert_tree(work_tree)
+        output = [e.name for e in
+            tree.iter_child_entries(tree.get_root_id())]
+        self.assertEqual(set(['a', 'f']), set(output))
+        output = [e.name for e in
+            tree.iter_child_entries(tree.path2id('a'))]
+        self.assertEqual(set(['b', 'd']), set(output))
+
+    def test_does_not_exist(self):
+        work_tree = self.make_branch_and_tree('.')
+        self.build_tree(['a/'])
+        work_tree.add(['a'])
+        tree = self._convert_tree(work_tree)
+        self.assertRaises(errors.NoSuchId, lambda:
+            list(tree.iter_child_entries('unknown')))
+
+
 class TestHasId(TestCaseWithTree):
 
     def test_has_id(self):

=== modified file 'bzrlib/transform.py'
--- a/bzrlib/transform.py	2012-03-14 10:17:12 +0000
+++ b/bzrlib/transform.py	2012-04-02 01:44:26 +0000
@@ -2182,6 +2182,14 @@
                 ordered_ids.append((trans_id, parent_file_id))
         return ordered_ids
 
+    def iter_child_entries(self, file_id, path=None):
+        self.id2path(file_id)
+        trans_id = self._transform.trans_id_file_id(file_id)
+        todo = [(child_trans_id, trans_id) for child_trans_id in
+                self._all_children(trans_id)]
+        for entry, trans_id in self._make_inv_entries(todo):
+            yield entry
+
     def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
         # This may not be a maximally efficient implementation, but it is
         # reasonably straightforward.  An implementation that grafts the

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2012-02-23 18:48:26 +0000
+++ b/bzrlib/tree.py	2012-04-02 01:44:26 +0000
@@ -195,6 +195,16 @@
         """
         raise NotImplementedError(self.iter_entries_by_dir)
 
+    def iter_child_entries(self, file_id, path=None):
+        """Iterate over the children of a directory or tree reference.
+
+        :param file_id: File id of the directory/tree-reference
+        :param path: Optional path of the directory
+        :raise NoSuchId: When the file_id does not exist
+        :return: Iterator over entries in the directory
+        """
+        raise NotImplementedError(self.iter_child_entries)
+
     def list_files(self, include_root=False, from_dir=None, recursive=True):
         """List all files in this tree.
 
@@ -871,6 +881,11 @@
         return self.root_inventory.iter_entries_by_dir(
             specific_file_ids=inventory_file_ids, yield_parents=yield_parents)
 
+    @needs_read_lock
+    def iter_child_entries(self, file_id, path=None):
+        inv, inv_file_id = self._unpack_file_id(file_id)
+        return inv[inv_file_id].children.itervalues()
+
     @deprecated_method(deprecated_in((2, 5, 0)))
     def get_file_by_path(self, path):
         return self.get_file(self.path2id(path), path)
@@ -1344,8 +1359,8 @@
                         if old_entry is None:
                             # Reusing a discarded change.
                             old_entry = self._get_entry(self.source, file_id)
-                        for child in self.source.iter_children(file_id):
-                            precise_file_ids.add(child)
+                        precise_file_ids.update(
+                                self.source.iter_children(file_id))
                     changed_file_ids.add(result[0])
                     yield result
 




More information about the bazaar-commits mailing list