Rev 2421: (broken) Add Tree.get_root_id() & test in http://sourcefrog.net/bzr/dirstate-plus-subtree

Martin Pool mbp at sourcefrog.net
Thu Mar 1 07:56:58 GMT 2007


At http://sourcefrog.net/bzr/dirstate-plus-subtree

------------------------------------------------------------
revno: 2421
revision-id: mbp at sourcefrog.net-20070301075656-srwqme1s2d721937
parent: mbp at sourcefrog.net-20070301055733-a977ktcx75cqtqyb
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: dirstate-plus-subtree
timestamp: Thu 2007-03-01 18:56:56 +1100
message:
  (broken) Add Tree.get_root_id() & test
  
  Add unique root on WorkingTree4 at construction time and test this: currently
  fails trying to set_path_id when the tree has parents.
modified:
  bzrlib/dirstate.py             dirstate.py-20060728012006-d6mvoihjb3je9peu-1
  bzrlib/revisiontree.py         revisiontree.py-20060724012533-bg8xyryhxd0o0i0h-1
  bzrlib/tests/test_workingtree_4.py test_workingtree_4.p-20070223025758-531n3tznl3zacv2o-1
  bzrlib/tests/tree_implementations/test_tree.py test_tree.py-20061215160206-usu7lwcj8aq2n3br-1
  bzrlib/workingtree_4.py        workingtree_4.py-20070208044105-5fgpc5j3ljlh5q6c-1
=== modified file 'bzrlib/dirstate.py'
--- a/bzrlib/dirstate.py	2007-03-01 03:44:56 +0000
+++ b/bzrlib/dirstate.py	2007-03-01 07:56:56 +0000
@@ -1824,6 +1824,8 @@
             # Remove it, its meaningless.
             block = self._find_block(current_old[0])
             entry_index, present = self._find_entry_index(current_old[0], block[1])
+            if not present:
+                import pdb;pdb.set_trace()
             assert present, 'could not find entry for %s' % (current_old,)
             block[1].pop(entry_index)
             # if we have an id_index in use, remove this key from it for this id.

=== modified file 'bzrlib/revisiontree.py'
--- a/bzrlib/revisiontree.py	2007-02-23 05:47:26 +0000
+++ b/bzrlib/revisiontree.py	2007-03-01 07:56:56 +0000
@@ -131,6 +131,10 @@
     def get_reference_revision(self, entry, path=None):
         return entry.reference_revision
 
+    def get_root_id(self):
+        if self.inventory.root:
+            return self.inventory.root.file_id
+
     def kind(self, file_id):
         file_id = osutils.safe_file_id(file_id)
         return self._inventory[file_id].kind

=== modified file 'bzrlib/tests/test_workingtree_4.py'
--- a/bzrlib/tests/test_workingtree_4.py	2007-03-01 05:57:33 +0000
+++ b/bzrlib/tests/test_workingtree_4.py	2007-03-01 07:56:56 +0000
@@ -435,3 +435,17 @@
         self.assertEqual(None, tree.id2path('a-id'))
         self.assertEqual('b', tree.id2path('b-id'))
         self.assertEqual(None, tree.id2path('c-id'))
+
+    def test_unique_root_id_per_tree(self):
+        # each time you initialize a new tree, it gets a different root id
+        format_name = 'experimental-reference-dirstate'
+        tree1 = self.make_branch_and_tree('tree1',
+            format=format_name)
+        tree2 = self.make_branch_and_tree('tree2',
+            format=format_name)
+        self.assertNotEqual(tree1.get_root_id(), tree2.get_root_id())
+        # when you branch, it inherits the same root id
+        rev1 = tree1.commit('first post')
+        tree3 = tree1.bzrdir.sprout('tree3').open_workingtree()
+        self.assertEqual(tree3.get_root_id(), tree1.get_root_id())
+

=== modified file 'bzrlib/tests/tree_implementations/test_tree.py'
--- a/bzrlib/tests/tree_implementations/test_tree.py	2007-02-26 14:24:46 +0000
+++ b/bzrlib/tests/tree_implementations/test_tree.py	2007-03-01 07:56:56 +0000
@@ -72,3 +72,10 @@
             tree.unlock()
         self.assertEqual([entry], [e for p, e in
                                    tree.iter_reference_entries()])
+
+    def test_get_root_id(self):
+        # trees should return some kind of root id; it can be none
+        tree = self.make_branch_and_tree('tree')
+        root_id = tree.get_root_id()
+        if root_id is not None:
+            self.assertIsInstance(root_id, str)

=== modified file 'bzrlib/workingtree_4.py'
--- a/bzrlib/workingtree_4.py	2007-03-01 04:07:42 +0000
+++ b/bzrlib/workingtree_4.py	2007-03-01 07:56:56 +0000
@@ -72,8 +72,6 @@
 import bzrlib.mutabletree
 from bzrlib.mutabletree import needs_tree_write_lock
 from bzrlib.osutils import (
-    compact_date,
-    file_kind,
     isdir,
     normpath,
     pathjoin,
@@ -81,7 +79,6 @@
     realpath,
     safe_unicode,
     splitpath,
-    supports_executable,
     )
 from bzrlib.trace import mutter, note
 from bzrlib.transport.local import LocalTransport
@@ -93,9 +90,6 @@
         deprecated_method,
         deprecated_function,
         DEPRECATED_PARAMETER,
-        zero_eight,
-        zero_eleven,
-        zero_thirteen,
         )
 from bzrlib.tree import Tree
 from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
@@ -1094,8 +1088,10 @@
     def initialize(self, a_bzrdir, revision_id=None):
         """See WorkingTreeFormat.initialize().
 
-        revision_id allows creating a working tree at a different
+        :param revision_id: allows creating a working tree at a different
         revision than the branch is at.
+
+        These trees get an initial random root id.
         """
         revision_id = osutils.safe_revision_id(revision_id)
         if not isinstance(a_bzrdir.transport, LocalTransport):
@@ -1109,6 +1105,7 @@
         if revision_id is None:
             revision_id = branch.last_revision()
         local_path = transport.local_abspath('dirstate')
+        # write out new dirstate (must exist when we create the tree)
         state = dirstate.DirState.initialize(local_path)
         state.unlock()
         wt = WorkingTree4(a_bzrdir.root_transport.local_abspath('.'),
@@ -1119,11 +1116,20 @@
         wt._new_tree()
         wt.lock_tree_write()
         try:
-            #wt.current_dirstate().set_path_id('', NEWROOT)
             wt.set_last_revision(revision_id)
             wt.flush()
             basis = wt.basis_tree()
             basis.lock_read()
+            state = wt.current_dirstate()
+            # if the basis has a root id we have to use that; otherwise we use
+            # a new random one
+            basis_root_id = basis.get_root_id()
+            if basis_root_id is not None:
+                wt._set_root_id(basis_root_id)
+                wt.flush()
+            elif revision_id in (None, NULL_REVISION):
+                wt._set_root_id(generate_ids.gen_root_id())
+                wt.flush()
             transform.build_tree(basis, wt)
             basis.unlock()
         finally:
@@ -1186,6 +1192,9 @@
         pred = self.has_filename
         return set((p for p in paths if not pred(p)))
 
+    def get_root_id(self):
+        return self.path2id('')
+
     def _get_parent_index(self):
         """Return the index in the dirstate referenced by this tree."""
         return self._dirstate.get_parent_ids().index(self._revision_id) + 1




More information about the bazaar-commits mailing list