Rev 2389: Add a tree-test for get_symlink_target in http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate

John Arbash Meinel john at arbash-meinel.com
Sun Feb 25 16:57:38 GMT 2007


At http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate

------------------------------------------------------------
revno: 2389
revision-id: john at arbash-meinel.com-20070225165633-d9x1okv4g33idiya
parent: john at arbash-meinel.com-20070225162253-8expr1vlo70wxp72
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dirstate
timestamp: Sun 2007-02-25 10:56:33 -0600
message:
  Add a tree-test for get_symlink_target
  and implement it in DirStateRevisionTree.
added:
  bzrlib/tests/tree_implementations/test_get_symlink_target.py test_get_symlink_tar-20070225165554-ickod3w3t7u0zzqh-1
modified:
  bzrlib/tests/tree_implementations/__init__.py __init__.py-20060717075546-420s7b0bj9hzeowi-2
  bzrlib/tree.py                 tree.py-20050309040759-9d5f2496be663e77
  bzrlib/workingtree_4.py        workingtree_4.py-20070208044105-5fgpc5j3ljlh5q6c-1
-------------- next part --------------
=== added file 'bzrlib/tests/tree_implementations/test_get_symlink_target.py'
--- a/bzrlib/tests/tree_implementations/test_get_symlink_target.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/tree_implementations/test_get_symlink_target.py	2007-02-25 16:56:33 +0000
@@ -0,0 +1,50 @@
+# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Test that all Tree's implement get_symlink_target"""
+
+import os
+
+from bzrlib import (
+    errors,
+    osutils,
+    tests,
+    )
+from bzrlib.tests.tree_implementations import TestCaseWithTree
+
+
+class TestGetSymlinkTarget(TestCaseWithTree):
+
+    def get_tree_with_symlinks(self):
+        if not osutils.has_symlinks():
+            raise tests.TestSkipped('platform does not support symlinks.')
+
+        tree = self.make_branch_and_tree('tree')
+        os.symlink('foo', 'tree/link')
+        os.symlink('../bar', 'tree/rel_link')
+        os.symlink('/baz/bing', 'tree/abs_link')
+
+        tree.add(['link', 'rel_link', 'abs_link'],
+                 ['link-id', 'rel-link-id', 'abs-link-id'])
+        return self._convert_tree(tree)
+
+    def test_get_symlink_target(self):
+        tree = self.get_tree_with_symlinks()
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        self.assertEqual('foo', tree.get_symlink_target('link-id'))
+        self.assertEqual('../bar', tree.get_symlink_target('rel-link-id'))
+        self.assertEqual('/baz/bing', tree.get_symlink_target('abs-link-id'))

=== modified file 'bzrlib/tests/tree_implementations/__init__.py'
--- a/bzrlib/tests/tree_implementations/__init__.py	2007-02-22 15:39:23 +0000
+++ b/bzrlib/tests/tree_implementations/__init__.py	2007-02-25 16:56:33 +0000
@@ -295,6 +295,7 @@
 def test_suite():
     result = TestSuite()
     test_tree_implementations = [
+        'bzrlib.tests.tree_implementations.test_get_symlink_target',
         'bzrlib.tests.tree_implementations.test_list_files',
         'bzrlib.tests.tree_implementations.test_revision_tree',
         'bzrlib.tests.tree_implementations.test_test_trees',

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2007-02-23 05:12:06 +0000
+++ b/bzrlib/tree.py	2007-02-25 16:56:33 +0000
@@ -183,6 +183,16 @@
     def get_file_by_path(self, path):
         return self.get_file(self._inventory.path2id(path))
 
+    def get_symlink_target(self, file_id):
+        """Get the target for a given file_id.
+
+        It is assumed that the caller already knows that file_id is referencing
+        a symlink.
+        :param file_id: Handle for the symlink entry.
+        :return: The path the symlink points to.
+        """
+        raise NotImplementedError(self.get_symlink_target)
+
     def annotate_iter(self, file_id):
         """Return an iterator of revision_id, line tuples
 

=== modified file 'bzrlib/workingtree_4.py'
--- a/bzrlib/workingtree_4.py	2007-02-25 16:22:53 +0000
+++ b/bzrlib/workingtree_4.py	2007-02-25 16:56:33 +0000
@@ -1050,6 +1050,10 @@
         pred = self.has_filename
         return set((p for p in paths if not pred(p)))
 
+    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
+
     def _get_entry(self, file_id=None, path=None):
         """Get the dirstate row for file_id or path.
 
@@ -1066,7 +1070,7 @@
         file_id = osutils.safe_file_id(file_id)
         if path is not None:
             path = path.encode('utf8')
-        parent_index = self._dirstate.get_parent_ids().index(self._revision_id) + 1
+        parent_index = self._get_parent_index()
         return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
 
     def _generate_inventory(self):
@@ -1157,6 +1161,17 @@
     def get_file_text(self, file_id):
         return ''.join(self.get_file_lines(file_id))
 
+    def get_symlink_target(self, file_id):
+        entry = self._get_entry(file_id=file_id)
+        parent_index = self._get_parent_index()
+        if entry[1][parent_index][0] != 'l':
+            return None
+        else:
+            # At present, none of the tree implementations supports non-ascii
+            # symlink targets. So we will just assume that the dirstate path is
+            # correct.
+            return entry[1][parent_index][1]
+
     def get_revision_id(self):
         """Return the revision id for this tree."""
         return self._revision_id



More information about the bazaar-commits mailing list