Rev 2442: All trees should implement get_file_mtime() in http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate

John Arbash Meinel john at arbash-meinel.com
Thu Mar 1 00:48:55 GMT 2007


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

------------------------------------------------------------
revno: 2442
revision-id: john at arbash-meinel.com-20070301004745-2hwuavq5wgmi2idi
parent: john at arbash-meinel.com-20070228233639-kt8judz9w3x27ve4
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dirstate
timestamp: Wed 2007-02-28 18:47:45 -0600
message:
  All trees should implement get_file_mtime()
added:
  bzrlib/tests/tree_implementations/test_get_file_mtime.py test_get_file_mtime.-20070301003805-kjkpwfp4whbm39o1-1
modified:
  bzrlib/tests/tree_implementations/__init__.py __init__.py-20060717075546-420s7b0bj9hzeowi-2
  bzrlib/tree.py                 tree.py-20050309040759-9d5f2496be663e77
  bzrlib/workingtree.py          workingtree.py-20050511021032-29b6ec0a681e02e3
  bzrlib/workingtree_4.py        workingtree_4.py-20070208044105-5fgpc5j3ljlh5q6c-1
-------------- next part --------------
=== added file 'bzrlib/tests/tree_implementations/test_get_file_mtime.py'
--- a/bzrlib/tests/tree_implementations/test_get_file_mtime.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/tree_implementations/test_get_file_mtime.py	2007-03-01 00:47:45 +0000
@@ -0,0 +1,43 @@
+# 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_file_mtime"""
+
+import time
+
+from bzrlib.tests.tree_implementations import TestCaseWithTree
+
+
+class TestGetFileMTime(TestCaseWithTree):
+
+    def get_basic_tree(self):
+        tree = self.make_branch_and_tree('tree')
+        self.build_tree(['tree/one'])
+        tree.add(['one'], ['one-id'])
+        return self._convert_tree(tree)
+
+    def test_get_symlink_target(self):
+        now = time.time()
+        tree = self.get_basic_tree()
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        # Committed trees return the time of the commit that last changed the
+        # file, working trees return the on-disk time.
+        mtime_file_id = tree.get_file_mtime(file_id='one-id')
+        self.assertIsInstance(mtime_file_id, (float, int))
+        self.failUnless(now - 5 < mtime_file_id < now + 5)
+        mtime_path = tree.get_file_mtime(file_id='one-id', path='one')
+        self.assertEqual(mtime_file_id, mtime_path)

=== modified file 'bzrlib/tests/tree_implementations/__init__.py'
--- a/bzrlib/tests/tree_implementations/__init__.py	2007-02-25 16:56:33 +0000
+++ b/bzrlib/tests/tree_implementations/__init__.py	2007-03-01 00:47:45 +0000
@@ -295,6 +295,7 @@
 def test_suite():
     result = TestSuite()
     test_tree_implementations = [
+        'bzrlib.tests.tree_implementations.test_get_file_mtime',
         'bzrlib.tests.tree_implementations.test_get_symlink_target',
         'bzrlib.tests.tree_implementations.test_list_files',
         'bzrlib.tests.tree_implementations.test_revision_tree',

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2007-02-26 02:19:29 +0000
+++ b/bzrlib/tree.py	2007-03-01 00:47:45 +0000
@@ -180,7 +180,16 @@
     def get_file(self, file_id):
         """Return a file object for the file file_id in the tree."""
         raise NotImplementedError(self.get_file)
-    
+
+    def get_file_mtime(self, file_id, path=None):
+        """Return the modification time for a file.
+
+        :param file_id: The handle for this file.
+        :param path: The path that this file can be found at.
+            These must point to the same object.
+        """
+        raise NotImplementedError(self.get_file_mtime)
+
     def get_file_by_path(self, path):
         return self.get_file(self._inventory.path2id(path))
 

=== modified file 'bzrlib/workingtree.py'
--- a/bzrlib/workingtree.py	2007-02-26 04:54:23 +0000
+++ b/bzrlib/workingtree.py	2007-03-01 00:47:45 +0000
@@ -596,7 +596,7 @@
     def get_file_mtime(self, file_id, path=None):
         file_id = osutils.safe_file_id(file_id)
         if not path:
-            path = self._inventory.id2path(file_id)
+            path = self.inventory.id2path(file_id)
         return os.lstat(self.abspath(path)).st_mtime
 
     if not supports_executable():

=== modified file 'bzrlib/workingtree_4.py'
--- a/bzrlib/workingtree_4.py	2007-02-28 23:36:39 +0000
+++ b/bzrlib/workingtree_4.py	2007-03-01 00:47:45 +0000
@@ -1218,6 +1218,19 @@
                 parent_ie.children[name_unicode] = inv_entry
         self._inventory = inv
 
+    def get_file_mtime(self, file_id, path=None):
+        """Return the modification time for this record.
+
+        We return the timestamp of the last-changed revision.
+        """
+        # Make sure the file exists
+        entry = self._get_entry(file_id, path=path)
+        if entry == (None, None): # do we raise?
+            return None
+        parent_index = self._get_parent_index()
+        last_changed_revision = entry[1][parent_index][4]
+        return self._repository.get_revision(last_changed_revision).timestamp
+
     def get_file_sha1(self, file_id, path=None, stat_value=None):
         # TODO: if path is present, fast-path on that, as inventory
         # might not be present



More information about the bazaar-commits mailing list