[Pkg-bazaar-commits] ./bzr/unstable r155: add new explicit RootEntry to inventory (in-core only)

mbp at sourcefrog.net mbp at sourcefrog.net
Fri Apr 10 07:51:16 UTC 2009


------------------------------------------------------------
revno: 155
committer: mbp at sourcefrog.net
timestamp: Thu 2005-03-31 13:24:21 +1000
message:
  add new explicit RootEntry to inventory (in-core only)
modified:
  NEWS
  bzrlib/inventory.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-03-31 00:04:54 +0000
+++ b/NEWS	2005-03-31 03:24:21 +0000
@@ -1,3 +1,11 @@
+bzr-0.0.3  NOT RELEASED YET
+
+  INTERNAL:
+
+    * Refactored inventory storage to insert a root entry at the top.
+
+
+
 bzr-0.0.2  "black cube"  2003-03-31
 
   ENHANCEMENTS:

=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2005-03-30 22:27:17 +0000
+++ b/bzrlib/inventory.py	2005-03-31 03:24:21 +0000
@@ -199,6 +199,23 @@
 
 
 
+class RootEntry(InventoryEntry):
+    def __init__(self, file_id):
+        self.file_id = file_id
+        self.children = {}
+        self.kind = 'root_directory'
+        self.parent_id = None
+
+    def __cmp__(self, other):
+        if self is other:
+            return 0
+        if not isinstance(other, RootEntry):
+            return NotImplemented
+        return cmp(self.file_id, other.file_id) \
+               or cmp(self.children, other.children)
+
+
+
 class Inventory(XMLMixin):
     """Inventory of versioned files in a tree.
 
@@ -217,7 +234,7 @@
     returned quickly.
 
     InventoryEntry objects must not be modified after they are
-    inserted.
+    inserted, other than through the Inventory API.
 
     >>> inv = Inventory()
     >>> inv.write_xml(sys.stdout)
@@ -263,9 +280,12 @@
         If a working directory is specified, the inventory is read
         from there.  If the file is specified, read from that. If not,
         the inventory is created empty.
+
+        The inventory is created with a default root directory, with
+        an id of None.
         """
-        self._root = InventoryEntry(None, '', kind='directory')
-        self._byid = {None: self._root}
+        self.root = RootEntry(None)
+        self._byid = {None: self.root}
 
 
     def __iter__(self):
@@ -277,21 +297,29 @@
         return len(self._byid)
 
 
-    def iter_entries(self, parent_id=None):
+    def iter_entries(self, from_dir=None):
         """Return (path, entry) pairs, in order by name."""
-        kids = self[parent_id].children.items()
+        if from_dir == None:
+            assert self.root
+            from_dir = self.root
+        elif isinstance(from_dir, basestring):
+            from_dir = self._byid[from_dir]
+            
+        kids = from_dir.children.items()
         kids.sort()
         for name, ie in kids:
             yield name, ie
             if ie.kind == 'directory':
-                for cn, cie in self.iter_entries(parent_id=ie.file_id):
-                    yield joinpath([name, cn]), cie
-
-
-    def directories(self):
+                for cn, cie in self.iter_entries(from_dir=ie.file_id):
+                    yield '/'.join((name, cn)), cie
+                    
+
+
+    def directories(self, from_dir=None):
         """Return (path, entry) pairs for all directories.
         """
-        yield '', self._root
+        assert self.root
+        yield '', self.root
         for path, entry in self.iter_entries():
             if entry.kind == 'directory':
                 yield path, entry
@@ -323,10 +351,7 @@
 
 
     def get_child(self, parent_id, filename):
-        if parent_id == None:
-            return self._root.children.get(filename)
-        else:
-            return self[parent_id].children.get(filename)
+        return self[parent_id].children.get(filename)
 
 
     def add(self, entry):
@@ -337,9 +362,10 @@
         if entry.file_id in self._byid:
             bailout("inventory already contains entry with id {%s}" % entry.file_id)
 
-        parent = self._byid[entry.parent_id]
-        if parent.kind != 'directory':
-            bailout("attempt to add under non-directory {%s}" % parent.file_id)
+        try:
+            parent = self._byid[entry.parent_id]
+        except KeyError:
+            bailout("parent_id %r not in inventory" % entry.parent_id)
 
         if parent.children.has_key(entry.name):
             bailout("%s is already versioned" %



More information about the Pkg-bazaar-commits mailing list