Rev 179: Switch to using WT.iter_changes(WT.basis_tree()) in http://bazaar.launchpad.net/~jameinel/bzr-explorer/wt_model

John Arbash Meinel john at arbash-meinel.com
Thu Jul 9 20:03:36 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr-explorer/wt_model

------------------------------------------------------------
revno: 179
revision-id: john at arbash-meinel.com-20090709190330-ipcab6h6oh62awry
parent: john at arbash-meinel.com-20090709183314-bewoe20y3s4myix0
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: wt_model
timestamp: Thu 2009-07-09 14:03:30 -0500
message:
  Switch to using WT.iter_changes(WT.basis_tree())
  
  This gives us more status information, which makes it more interesting
  overall.
-------------- next part --------------
=== modified file 'lib/wt_model.py'
--- a/lib/wt_model.py	2009-07-09 18:33:14 +0000
+++ b/lib/wt_model.py	2009-07-09 19:03:30 +0000
@@ -16,7 +16,7 @@
 
 """Implement a Model defining interactions with a WorkingTree"""
 
-from bzrlib import workingtree
+from bzrlib import osutils, workingtree
 
 from PyQt4 import QtCore, QtGui
 
@@ -32,14 +32,14 @@
     :type children: A list of _WTItem objects
     """
 
-    def __init__(self, path, name, disk_kind, file_id, inv_kind, parent):
+    def __init__(self, path, name, kind, file_id, old_kind, parent):
         """Create a new _WTItem
         """
         self.path = path
         self.name = name
-        self.disk_kind = disk_kind
+        self.kind = kind
         self.file_id = file_id
-        self.inv_kind = inv_kind
+        self.old_kind = old_kind
         # Note: this creates a cyclical reference, consider using parent
         #       instead
         self.parent = parent
@@ -56,32 +56,46 @@
             return 0
         return self.parent.children.index(self)
 
-    @staticmethod
-    def create_from_wt(wt):
+    def _from_iter_changes(self, changes):
+        # By file-id
+        dir_by_id = {}
+        dir_by_path = {}
+        for (file_id, paths, did_change, versions, parents, names, kinds,
+             executables) in changes:
+            if file_id is None:
+                # not versioned
+                parent_dir = osutils.dirname(paths[1])
+                if names[1] == '': # unversioned root?
+                    raise ValueError('not supported yet')
+                parent = dir_by_path[parent_dir]
+            else:
+                if parents[1] is None: # Root entry
+                    # Or maybe this happens for unversioned files?
+                    assert names[1] == ''
+                    parent = self
+                else:
+                    parent = dir_by_id[parents[1]]
+            item = _WTItem(paths[1], names[1], kinds[1], file_id, kinds[0],
+                           parent)
+            parent.children.append(item)
+            if kinds[1] == 'directory':
+                dir_by_id[file_id] = item
+                dir_by_path[paths[1]] = item
+
+    @classmethod
+    def create_from_wt(cls, wt):
         """Create the _WTItem tree structure from an inventory."""
         wt.lock_read()
         try:
-            root_wt_item = _WTItem(None, None, None, None, None, None)
-            # By file-id
-            dir_items = {}
-            for dir_info, dir_block_info in wt.walkdirs():
-                dir_path, dir_file_id = dir_info
-                try:
-                    dir_wt_item = dir_items[dir_file_id]
-                except KeyError:
-                    # Create the directory root ide
-                    dir_wt_item = _WTItem(dir_path, '', 'directory',
-                        dir_file_id, 'directory', parent=None)
-                    dir_items[dir_file_id] = dir_wt_item
-                    root_wt_item.children.append(dir_wt_item)
-                for (f_path, f_name, f_disk_kind, _, f_id,
-                     f_inv_kind) in dir_block_info:
-                    wt_item = _WTItem(f_path, f_name, f_disk_kind, f_id,
-                                      f_inv_kind, dir_wt_item)
-                    dir_wt_item.children.append(wt_item)
-                    # TODO: handle directories that aren't versioned
-                    if f_inv_kind == 'directory':
-                        dir_items[f_id] = wt_item
+            basis_tree = wt.basis_tree()
+            basis_tree.lock_read()
+            try:
+                root_wt_item = cls(None, None, None, None, None, None)
+                changes = wt.iter_changes(basis_tree, include_unchanged=True,
+                    require_versioned=False, want_unversioned=True)
+                root_wt_item._from_iter_changes(changes)
+            finally:
+                basis_tree.unlock()
         finally:
             wt.unlock()
         return root_wt_item
@@ -92,9 +106,9 @@
     _headers = [
         'name',
         'file_id',
-        'disk_kind',
+        'kind',
         'status',
-        'inv_kind',
+        'old_kind',
         'path',
         ]
     _column_to_attribute = dict(enumerate(_headers))

=== modified file 'tests/test_wt_model.py'
--- a/tests/test_wt_model.py	2009-07-09 18:33:14 +0000
+++ b/tests/test_wt_model.py	2009-07-09 19:03:30 +0000
@@ -199,11 +199,11 @@
         res = model.headerData(1, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
         self.assertEqual('file_id', res.toString())
         res = model.headerData(2, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
-        self.assertEqual('disk_kind', res.toString())
+        self.assertEqual('kind', res.toString())
         res = model.headerData(3, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
         self.assertEqual('status', res.toString())
         res = model.headerData(4, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
-        self.assertEqual('inv_kind', res.toString())
+        self.assertEqual('old_kind', res.toString())
         res = model.headerData(5, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
         self.assertEqual('path', res.toString())
 
@@ -227,22 +227,22 @@
         wt.set_root_id('root-id')
         root_item = wt_model._WTItem.create_from_wt(wt)
         self.assertIsInstance(root_item, wt_model._WTItem)
-        self.assertEqual(None, root_item.path)
+        self.assertIs(None, root_item.path)
         self.assertEqual(1, len(root_item.children))
         tree_root_item = root_item.children[0]
         self.assertEqual('', tree_root_item.path)
         self.assertEqual('', tree_root_item.name)
-        self.assertEqual('directory', tree_root_item.disk_kind)
+        self.assertEqual('directory', tree_root_item.kind)
         self.assertEqual('root-id', tree_root_item.file_id)
-        self.assertEqual('directory', tree_root_item.inv_kind)
-        self.assertIs(None, tree_root_item.parent)
+        self.assertIs(None, tree_root_item.old_kind)
+        self.assertIs(root_item, tree_root_item.parent)
         self.assertEqual(1, len(tree_root_item.children))
         child = tree_root_item.children[0]
         self.assertEqual('a_file', child.path)
         self.assertEqual('a_file', child.name)
-        self.assertEqual('file', child.disk_kind)
+        self.assertEqual('file', child.kind)
         self.assertEqual('file-id', child.file_id)
-        self.assertEqual('file', child.inv_kind)
+        self.assertIs(None, child.old_kind)
         self.assertEqual(tree_root_item, child.parent)
         self.assertEqual(0, child.row())
 
@@ -253,31 +253,31 @@
         wt.add(['a_file'], ['file-id'])
         root_item = wt_model._WTItem.create_from_wt(wt)
         self.assertIsInstance(root_item, wt_model._WTItem)
-        self.assertEqual(None, root_item.path)
+        self.assertIs(None, root_item.path)
         self.assertEqual(1, len(root_item.children))
         tree_root_item = root_item.children[0]
         self.assertIsInstance(tree_root_item, wt_model._WTItem)
         self.assertEqual('', tree_root_item.path)
         self.assertEqual('', tree_root_item.name)
-        self.assertEqual('directory', tree_root_item.disk_kind)
+        self.assertEqual('directory', tree_root_item.kind)
         self.assertEqual('root-id', tree_root_item.file_id)
-        self.assertEqual('directory', tree_root_item.inv_kind)
-        self.assertIs(None, tree_root_item.parent)
+        self.assertIs(None, tree_root_item.old_kind)
+        self.assertIs(root_item, tree_root_item.parent)
         self.assertEqual(2, len(tree_root_item.children))
         child = tree_root_item.children[0]
         self.assertEqual('a_file', child.path)
         self.assertEqual('a_file', child.name)
-        self.assertEqual('file', child.disk_kind)
+        self.assertEqual('file', child.kind)
         self.assertEqual('file-id', child.file_id)
-        self.assertEqual('file', child.inv_kind)
+        self.assertIs(None, child.old_kind)
         self.assertEqual(tree_root_item, child.parent)
         self.assertEqual(0, child.row())
         child = tree_root_item.children[1]
         self.assertEqual('unversioned-file', child.path)
         self.assertEqual('unversioned-file', child.name)
-        self.assertEqual('file', child.disk_kind)
-        self.assertEqual(None, child.file_id)
-        self.assertEqual(None, child.inv_kind)
+        self.assertEqual('file', child.kind)
+        self.assertIs(None, child.file_id)
+        self.assertIs(None, child.old_kind)
         self.assertEqual(tree_root_item, child.parent)
         self.assertEqual(1, child.row())
 
@@ -290,19 +290,19 @@
                ['dir-id', 'subdir-id', 'file-id'])
         root_item = wt_model._WTItem.create_from_wt(wt)
         self.assertIsInstance(root_item, wt_model._WTItem)
-        self.assertEqual(None, root_item.path)
+        self.assertIs(None, root_item.path)
         self.assertEqual(1, len(root_item.children))
         tree_root_item = root_item.children[0]
         self.assertIsInstance(tree_root_item, wt_model._WTItem)
         self.assertEqual('root-id', tree_root_item.file_id)
-        self.assertIs(None, tree_root_item.parent)
+        self.assertIs(root_item, tree_root_item.parent)
         self.assertEqual(1, len(tree_root_item.children))
         child = tree_root_item.children[0]
         self.assertEqual('dir', child.path)
         self.assertEqual('dir', child.name)
-        self.assertEqual('directory', child.disk_kind)
+        self.assertEqual('directory', child.kind)
         self.assertEqual('dir-id', child.file_id)
-        self.assertEqual('directory', child.inv_kind)
+        self.assertIs(None, child.old_kind)
         self.assertEqual(tree_root_item, child.parent)
         self.assertEqual(0, tree_root_item.row())
         self.assertEqual(1, len(child.children))
@@ -310,9 +310,9 @@
         child = this_dir.children[0]
         self.assertEqual('dir/subdir', child.path)
         self.assertEqual('subdir', child.name)
-        self.assertEqual('directory', child.disk_kind)
+        self.assertEqual('directory', child.kind)
         self.assertEqual('subdir-id', child.file_id)
-        self.assertEqual('directory', child.inv_kind)
+        self.assertIs(None, child.old_kind)
         self.assertEqual(this_dir, child.parent)
         self.assertEqual(0, child.row())
         self.assertEqual(1, len(child.children))
@@ -320,8 +320,8 @@
         child = this_dir.children[0]
         self.assertEqual('dir/subdir/a_file', child.path)
         self.assertEqual('a_file', child.name)
-        self.assertEqual('file', child.disk_kind)
+        self.assertEqual('file', child.kind)
         self.assertEqual('file-id', child.file_id)
-        self.assertEqual('file', child.inv_kind)
+        self.assertIs(None, child.old_kind)
         self.assertEqual(this_dir, child.parent)
         self.assertEqual(0, child.row())



More information about the bazaar-commits mailing list