Merge lp:~jelmer/bzr/less-inventory-access into lp:bzr

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: no longer in the source branch.
Merged at revision: 6470
Proposed branch: lp:~jelmer/bzr/less-inventory-access
Merge into: lp:bzr
Diff against target: 184 lines (+22/-39)
7 files modified
bzrlib/builtins.py (+0/-2)
bzrlib/bundle/bundle_data.py (+6/-8)
bzrlib/commit.py (+1/-3)
bzrlib/inventory.py (+0/-16)
bzrlib/merge.py (+2/-2)
bzrlib/tests/test_bundle.py (+9/-6)
bzrlib/workingtree.py (+4/-2)
To merge this branch: bzr merge lp:~jelmer/bzr/less-inventory-access
Reviewer Review Type Date Requested Status
Vincent Ladeuil Approve
Review via email: mp+93612@code.launchpad.net

Commit message

Avoid direct access of tree inventories in a couple more places.

Description of the change

Remove access to Tree.root_inventory in a couple of places.

In a few places we weren't actually using the inventory after assigning it to a local variable.

I've removed Inventory.directories(). It didn't have any tests and only one caller. Simply using Tree.iter_entries_by_dir() instead and filtering for directories works too, and should be just as quick.

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

Looking at the annotation, directories is so old... I think we're safe to delete it, but you'll get the blame for not deprecating it if somebody was using it ;)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py 2012-02-14 17:22:37 +0000
+++ bzrlib/builtins.py 2012-02-17 17:04:19 +0000
@@ -1650,10 +1650,8 @@
1650 def run(self, dir=u'.'):1650 def run(self, dir=u'.'):
1651 tree = WorkingTree.open_containing(dir)[0]1651 tree = WorkingTree.open_containing(dir)[0]
1652 self.add_cleanup(tree.lock_read().unlock)1652 self.add_cleanup(tree.lock_read().unlock)
1653 new_inv = tree.root_inventory
1654 old_tree = tree.basis_tree()1653 old_tree = tree.basis_tree()
1655 self.add_cleanup(old_tree.lock_read().unlock)1654 self.add_cleanup(old_tree.lock_read().unlock)
1656 old_inv = old_tree.root_inventory
1657 renames = []1655 renames = []
1658 iterator = tree.iter_changes(old_tree, include_unchanged=True)1656 iterator = tree.iter_changes(old_tree, include_unchanged=True)
1659 for f, paths, c, v, p, n, k, e in iterator:1657 for f, paths, c, v, p, n, k, e in iterator:
16601658
=== modified file 'bzrlib/bundle/bundle_data.py'
--- bzrlib/bundle/bundle_data.py 2012-01-24 16:19:04 +0000
+++ bzrlib/bundle/bundle_data.py 2012-02-17 17:04:19 +0000
@@ -687,10 +687,9 @@
687 if new_path not in self.patches:687 if new_path not in self.patches:
688 # If the entry does not have a patch, then the688 # If the entry does not have a patch, then the
689 # contents must be the same as in the base_tree689 # contents must be the same as in the base_tree
690 ie = self.base_tree.root_inventory[file_id]690 text_size = self.base_tree.get_file_size(file_id)
691 if ie.text_size is None:691 text_sha1 = self.base_tree.get_file_sha1(file_id)
692 return ie.text_size, ie.text_sha1692 return text_size, text_sha1
693 return int(ie.text_size), ie.text_sha1
694 fileobj = self.get_file(file_id)693 fileobj = self.get_file(file_id)
695 content = fileobj.read()694 content = fileobj.read()
696 return len(content), sha_string(content)695 return len(content), sha_string(content)
@@ -701,7 +700,6 @@
701 This need to be called before ever accessing self.inventory700 This need to be called before ever accessing self.inventory
702 """701 """
703 from os.path import dirname, basename702 from os.path import dirname, basename
704 base_inv = self.base_tree.root_inventory
705 inv = Inventory(None, self.revision_id)703 inv = Inventory(None, self.revision_id)
706704
707 def add_entry(file_id):705 def add_entry(file_id):
@@ -750,9 +748,9 @@
750748
751 root_inventory = property(_get_inventory)749 root_inventory = property(_get_inventory)
752750
753 def __iter__(self):751 def all_file_ids(self):
754 for path, entry in self.inventory.iter_entries():752 return set(
755 yield entry.file_id753 [entry.file_id for path, entry in self.inventory.iter_entries()])
756754
757 def list_files(self, include_root=False, from_dir=None, recursive=True):755 def list_files(self, include_root=False, from_dir=None, recursive=True):
758 # The only files returned by this are those from the version756 # The only files returned by this are those from the version
759757
=== modified file 'bzrlib/commit.py'
--- bzrlib/commit.py 2012-02-06 23:38:33 +0000
+++ bzrlib/commit.py 2012-02-17 17:04:19 +0000
@@ -834,11 +834,9 @@
834 deleted_paths = {}834 deleted_paths = {}
835 # XXX: Note that entries may have the wrong kind because the entry does835 # XXX: Note that entries may have the wrong kind because the entry does
836 # not reflect the status on disk.836 # not reflect the status on disk.
837 # FIXME: Nested trees
838 work_inv = self.work_tree.root_inventory
839 # NB: entries will include entries within the excluded ids/paths837 # NB: entries will include entries within the excluded ids/paths
840 # because iter_entries_by_dir has no 'exclude' facility today.838 # because iter_entries_by_dir has no 'exclude' facility today.
841 entries = work_inv.iter_entries_by_dir(839 entries = self.work_tree.iter_entries_by_dir(
842 specific_file_ids=self.specific_file_ids, yield_parents=True)840 specific_file_ids=self.specific_file_ids, yield_parents=True)
843 for path, existing_ie in entries:841 for path, existing_ie in entries:
844 file_id = existing_ie.file_id842 file_id = existing_ie.file_id
845843
=== modified file 'bzrlib/inventory.py'
--- bzrlib/inventory.py 2011-12-18 12:46:49 +0000
+++ bzrlib/inventory.py 2012-02-17 17:04:19 +0000
@@ -850,22 +850,6 @@
850 descend(self.root, u'')850 descend(self.root, u'')
851 return accum851 return accum
852852
853 def directories(self):
854 """Return (path, entry) pairs for all directories, including the root.
855 """
856 accum = []
857 def descend(parent_ie, parent_path):
858 accum.append((parent_path, parent_ie))
859
860 kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
861 kids.sort()
862
863 for name, child_ie in kids:
864 child_path = osutils.pathjoin(parent_path, name)
865 descend(child_ie, child_path)
866 descend(self.root, u'')
867 return accum
868
869 def path2id(self, relpath):853 def path2id(self, relpath):
870 """Walk down through directories to return entry of last component.854 """Walk down through directories to return entry of last component.
871855
872856
=== modified file 'bzrlib/merge.py'
--- bzrlib/merge.py 2012-01-24 16:19:04 +0000
+++ bzrlib/merge.py 2012-02-17 17:04:19 +0000
@@ -1260,9 +1260,9 @@
12601260
1261 def merge_names(self, file_id):1261 def merge_names(self, file_id):
1262 def get_entry(tree):1262 def get_entry(tree):
1263 if tree.has_id(file_id):1263 try:
1264 return tree.root_inventory[file_id]1264 return tree.root_inventory[file_id]
1265 else:1265 except errors.NoSuchId:
1266 return None1266 return None
1267 this_entry = get_entry(self.this_tree)1267 this_entry = get_entry(self.this_tree)
1268 other_entry = get_entry(self.other_tree)1268 other_entry = get_entry(self.other_tree)
12691269
=== modified file 'bzrlib/tests/test_bundle.py'
--- bzrlib/tests/test_bundle.py 2012-01-30 14:18:22 +0000
+++ bzrlib/tests/test_bundle.py 2012-02-17 17:04:19 +0000
@@ -80,9 +80,6 @@
80 def get_root_id(self):80 def get_root_id(self):
81 return self.root.file_id81 return self.root.file_id
8282
83 def get_root_id(self):
84 return self.root.file_id
85
86 def all_file_ids(self):83 def all_file_ids(self):
87 return set(self.paths.keys())84 return set(self.paths.keys())
8885
@@ -114,8 +111,8 @@
114 return kind111 return kind
115112
116 def make_entry(self, file_id, path):113 def make_entry(self, file_id, path):
117 from bzrlib.inventory import (InventoryEntry, InventoryFile114 from bzrlib.inventory import (InventoryFile , InventoryDirectory,
118 , InventoryDirectory, InventoryLink)115 InventoryLink)
119 name = os.path.basename(path)116 name = os.path.basename(path)
120 kind = self.kind(file_id)117 kind = self.kind(file_id)
121 parent_id = self.parent_id(file_id)118 parent_id = self.parent_id(file_id)
@@ -158,6 +155,12 @@
158 def get_file_revision(self, file_id):155 def get_file_revision(self, file_id):
159 return self.inventory[file_id].revision156 return self.inventory[file_id].revision
160157
158 def get_file_size(self, file_id):
159 return self.inventory[file_id].text_size
160
161 def get_file_sha1(self, file_id):
162 return self.inventory[file_id].text_sha1
163
161 def contents_stats(self, file_id):164 def contents_stats(self, file_id):
162 if file_id not in self.contents:165 if file_id not in self.contents:
163 return None, None166 return None, None
@@ -326,7 +329,7 @@
326 self.assertTrue(btree.path2id("grandparent/parent/file") is None)329 self.assertTrue(btree.path2id("grandparent/parent/file") is None)
327330
328 def sorted_ids(self, tree):331 def sorted_ids(self, tree):
329 ids = list(tree)332 ids = list(tree.all_file_ids())
330 ids.sort()333 ids.sort()
331 return ids334 return ids
332335
333336
=== modified file 'bzrlib/workingtree.py'
--- bzrlib/workingtree.py 2012-01-30 14:18:22 +0000
+++ bzrlib/workingtree.py 2012-02-17 17:04:19 +0000
@@ -2084,7 +2084,7 @@
2084 return osutils.lexists(self.abspath(path))2084 return osutils.lexists(self.abspath(path))
20852085
2086 def has_or_had_id(self, file_id):2086 def has_or_had_id(self, file_id):
2087 if file_id == self.root_inventory.root.file_id:2087 if file_id == self.get_root_id():
2088 return True2088 return True
2089 inv, inv_file_id = self._unpack_file_id(file_id)2089 inv, inv_file_id = self._unpack_file_id(file_id)
2090 return inv.has_id(inv_file_id)2090 return inv.has_id(inv_file_id)
@@ -2912,7 +2912,9 @@
2912 This is the same order used by 'osutils.walkdirs'.2912 This is the same order used by 'osutils.walkdirs'.
2913 """2913 """
2914 ## TODO: Work from given directory downwards2914 ## TODO: Work from given directory downwards
2915 for path, dir_entry in self.root_inventory.directories():2915 for path, dir_entry in self.iter_entries_by_dir():
2916 if dir_entry.kind != 'directory':
2917 continue
2916 # mutter("search for unknowns in %r", path)2918 # mutter("search for unknowns in %r", path)
2917 dirabs = self.abspath(path)2919 dirabs = self.abspath(path)
2918 if not isdir(dirabs):2920 if not isdir(dirabs):