[PATCH 3 of 7] revlog: add support for 'original text' to addrevision and addgroup

Sune Foldager cryo at cyanite.org
Tue Sep 14 04:56:32 CDT 2010


# HG changeset patch
# User Sune Foldager <cryo at cyanite.org>
# Date 1284133180 -7200
# Node ID 6d45223c5d5b9c6375f111d95ae711e689d0ecd1
# Parent  231111b93160405441ee00f0270e7249d931ab8e
revlog: add support for 'original text' to addrevision and addgroup

This allows derived revlogs to store a different text from the one accepted and
returned via the API. This can be used for custom compressed entries, such as
for light-weight copy.

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -1130,7 +1130,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
@@ -1138,8 +1139,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
@@ -1150,14 +1152,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
@@ -1172,7 +1174,7 @@
             deltarev, deltanode = prev, self.node(prev)
 
         # should we try to build a delta?
-        if deltarev != nullrev:
+        if deltarev != nullrev and not hashtext:
             # can we use the cached delta?
             if cachedelta:
                 cacherev, d = cachedelta
@@ -1195,7 +1197,9 @@
             l = len(data[1]) + len(data[0])
             base = curr
 
-        e = (offset_type(offset, flags), l, len(text),
+        if hashtext is None:
+            hashtext = text
+        e = (offset_type(offset, flags), l, len(hashtext),
              base, link, self.rev(p1), self.rev(p2), node)
         self.index.insert(-1, e)
         self.nodemap[node] = curr
@@ -1217,10 +1221,15 @@
             ifh.write(data[1])
             self.checkinlinesize(transaction, ifh)
 
-        if type(text) == str: # only accept immutable objects
-            self._cache = (node, curr, 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).
@@ -1355,10 +1364,10 @@
                         dfh.flush()
                     ifh.flush()
                     text = self.revision(chain)
-                    text = mdiff.patch(text, delta)
+                    text, hashtext = self._textfromdelta(text, delta)
                     del delta
                     chk = self._addrevision(node, text, transaction, link,
-                                            p1, p2, None, ifh, dfh)
+                                            p1, p2, None, hashtext, ifh, dfh)
                     if not dfh and not self._inline:
                         # addrevision switched from inline to conventional
                         # reopen the index
@@ -1366,7 +1375,9 @@
                         ifh = self.opener(self.indexfile, "a")
                     if chk != node:
                         raise RevlogError(_("consistency error adding group"))
-                    textlen = len(text)
+                    if hashtext is None:
+                        hashtext = text
+                    textlen = len(hashtext)
                 else:
                     e = (offset_type(end, 0), cdeltalen, textlen, base,
                          link, self.rev(p1), self.rev(p2), node)
@@ -1397,6 +1408,9 @@
 
         return node
 
+    def _textfromdelta(self, text, delta):
+        return mdiff.patch(text, delta), None
+
     def strip(self, minlink, transaction):
         """truncate the revlog on the first revision with a linkrev >= minlink
 


More information about the Mercurial-devel mailing list