Rev 1: Adding a command to verify the dirstate hash cache in http://bzr.arbash-meinel.com/plugins/rebuild_hash_cache

John Arbash Meinel john at arbash-meinel.com
Thu May 10 17:44:30 BST 2007


At http://bzr.arbash-meinel.com/plugins/rebuild_hash_cache

------------------------------------------------------------
revno: 1
revision-id: john at arbash-meinel.com-20070510164423-ftnwrmz6bp9t7fa7
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: rebuild_hash_cache
timestamp: Thu 2007-05-10 11:44:23 -0500
message:
  Adding a command to verify the dirstate hash cache
added:
  __init__.py                    __init__.py-20070510163923-c34sctk7g574a37g-1
-------------- next part --------------
=== added file '__init__.py'
--- a/__init__.py	1970-01-01 00:00:00 +0000
+++ b/__init__.py	2007-05-10 16:44:23 +0000
@@ -0,0 +1,69 @@
+# Copyright (C) 2007 by 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
+"""Make sure all sha hashes are up to date"""
+
+import os
+
+from bzrlib import (
+    commands,
+    errors,
+    workingtree,
+    )
+
+
+class cmd_rebuild_hash_cache(commands.Command):
+    """Make sure all cached records are updated."""
+
+    encoding_type = 'replace'
+
+    def run(self):
+        wt = workingtree.WorkingTree.open_containing('.')[0]
+        update_count = 0
+        wt.lock_write()
+        try:
+            dirstate = getattr(wt, '_dirstate', None)
+            if not dirstate:
+                raise errors.BzrCommandError(
+                    'Currently only WT4 and newer are supported')
+            # Force the dirstate to always re-read the sha hash
+            # All files newer than 1s from epoch should be read
+            dirstate._cutoff_time = 1
+            for entry in dirstate._iter_entries():
+                dirname, basename, file_id = entry[0]
+                if dirname:
+                    path = (dirname + '/' + basename).decode('utf8')
+                else:
+                    path = basename.decode('utf8')
+                abspath = wt.abspath(path)
+                stat_value = os.lstat(abspath)
+                # Information about the working tree
+                minikind, link_or_sha1, size, executable, stat = entry[1][0]
+                if minikind == 'f':
+                    cur_sha1 = link_or_sha1
+                    dirstate.update_entry(entry, abspath, stat_value)
+                    new_sha1 = entry[1][0][1]
+                    if cur_sha1 != new_sha1:
+                        self.outf.write(u'Updated %s (%s => %s)\n'
+                                        % (path, cur_sha1, new_sha1))
+                        update_count += 1
+            self.outf.write('Updated %d entries\n' % (update_count,))
+            if update_count > 0:
+                dirstate.save()
+        finally:
+            wt.unlock()
+
+
+commands.register_command(cmd_rebuild_hash_cache)



More information about the bazaar-commits mailing list