[PATCH 06 of 10] hash calculation backwards compatibility

Sune Foldager cryo at cyanite.org
Wed Sep 8 14:48:19 CDT 2010


# HG changeset patch
# User Sune Foldager <sune.foldager at edlund.dk>
# Date 1283860987 -7200
# Node ID 83e5bbba807175984580f2aa2c55d072826d3df0
# Parent  c7d4e0bec4be3d1a051ccf24a546253f6e9236e9
hash calculation backwards compatibility
* * *
Fix caching wrong text
* * *
text unpacking needs instance of str

diff --git a/mercurial/filelog.py b/mercurial/filelog.py
--- a/mercurial/filelog.py
+++ b/mercurial/filelog.py
@@ -5,7 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import revlog
+import revlog, mdiff
 
 class filelog(revlog.revlog):
     def __init__(self, opener, path):
@@ -68,6 +68,25 @@
 
         return True
 
+    def _ptext(self, meta):
+        return filelog(self.opener, meta["copy"]).read(revlog.bin(meta["copyrev"]))
+
+    def _unpack_text(self, text):
+        meta = _parsemeta(text)
+        if "lwcopy" in meta:
+            ptext = self._ptext(meta)
+            s = text.index('\1\n', 2)
+            text = mdiff.patches(ptext, [buffer(text, s + 2)])
+            # delete lwcopy entry to get just what we should get without lwcopy
+            del meta["lwcopy"]
+            text = "\1\n%s\1\n%s" % (_packmeta(meta), text)
+        return text
+
+    def _hash(self, text, hashtext, p1, p2):
+        if hashtext is None:
+            hashtext = self._unpack_text(text)
+        return revlog.revlog._hash(self, text, hashtext, p1, p2)
+
 def _parsemeta(t):
     # t can be buffer, so we can't use .startswith
     if t[:2] != '\1\n':
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -1135,7 +1135,8 @@
         tr.replace(self.indexfile, trindex * self._io.size)
         self._chunkclear()
 
-    def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
+    def addrevision(self, text, transaction, link, p1, p2, cachedelta=None,
+                    hashtext=None):
         """add a revision to the log
 
         text - the revision data to add
@@ -1143,8 +1144,9 @@
         link - the linkrev data to add
         p1, p2 - the parent nodeids of the revision
         cachedelta - an optional precomputed delta
+        hashtext - optional text to use for hash calculations instead of text
         """
-        node = hash(text, p1, p2)
+        node = self._hash(text, hashtext, p1, p2)
         if (node in self.nodemap and
             (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
             return node
@@ -1155,14 +1157,14 @@
         ifh = self.opener(self.indexfile, "a+")
         try:
             return self._addrevision(node, text, transaction, link, p1, p2,
-                                     cachedelta, ifh, dfh)
+                                     cachedelta, hashtext, ifh, dfh)
         finally:
             if dfh:
                 dfh.close()
             ifh.close()
 
     def _addrevision(self, node, text, transaction, link, p1, p2,
-                     cachedelta, ifh, dfh):
+                     cachedelta, hashtext, ifh, dfh):
         curr = len(self)
         prev = curr - 1
         base = curr
@@ -1222,10 +1224,17 @@
             ifh.write(data[1])
             self.checkinlinesize(transaction, ifh)
 
-        if type(text) == str: # only accept immutable objects
-            self._cache = (node, curr, text)
+        if hashtext is None:
+            hashtext = text
+        if type(hashtext) == str: # only accept immutable objects
+            self._cache = (node, curr, hashtext)
         return node
 
+    def _hash(self, text, hashtext, p1, p2):
+        if hashtext is None:
+            hashtext = text
+        return hash(hashtext, p1, p2)
+
     def group(self, nodelist, lookup, infocollect=None, fullrev=False):
         """Calculate a delta group, yielding a sequence of changegroup chunks
         (strings).
@@ -1363,7 +1372,7 @@
                     text = mdiff.patch(text, delta)
                     del delta
                     chk = self._addrevision(node, text, transaction, link,
-                                            p1, p2, None, ifh, dfh)
+                                            p1, p2, None, None, ifh, dfh)
                     if not dfh and not self._inline:
                         # addrevision switched from inline to conventional
                         # reopen the index


More information about the Mercurial-devel mailing list