storageutil: extract most of peek_censored from revlog
authorGregory Szorc <gregory.szorc@gmail.com>
Tue, 16 Oct 2018 15:36:19 +0200
changeset 40325 b0fbd1792e2d
parent 40324 6637b079ae45
child 40326 fed697fa1734
storageutil: extract most of peek_censored from revlog This function is super hacky and isn't correct 100% of the time. I'm going to need this functionality on a future non-revlog store. Let's copy things to storageutil so this code only exists once. Differential Revision: https://phab.mercurial-scm.org/D5118
mercurial/revlog.py
mercurial/utils/storageutil.py
--- a/mercurial/revlog.py	Thu Sep 20 17:27:01 2018 -0700
+++ b/mercurial/revlog.py	Tue Oct 16 15:36:19 2018 +0200
@@ -2109,23 +2109,7 @@
         if not self._censorable:
             return False
 
-        # Fragile heuristic: unless new file meta keys are added alphabetically
-        # preceding "censored", all censored revisions are prefixed by
-        # "\1\ncensored:". A delta producing such a censored revision must be a
-        # full-replacement delta, so we inspect the first and only patch in the
-        # delta for this prefix.
-        hlen = struct.calcsize(">lll")
-        if len(delta) <= hlen:
-            return False
-
-        oldlen = self.rawsize(baserev)
-        newlen = len(delta) - hlen
-        if delta[:hlen] != mdiff.replacediffheader(oldlen, newlen):
-            return False
-
-        add = "\1\ncensored:"
-        addlen = len(add)
-        return newlen >= addlen and delta[hlen:hlen + addlen] == add
+        return storageutil.deltaiscensored(delta, baserev, self.rawsize)
 
     def getstrippoint(self, minlink):
         """find the minimum rev that must be stripped to strip the linkrev
--- a/mercurial/utils/storageutil.py	Thu Sep 20 17:27:01 2018 -0700
+++ b/mercurial/utils/storageutil.py	Tue Oct 16 15:36:19 2018 +0200
@@ -9,6 +9,7 @@
 
 import hashlib
 import re
+import struct
 
 from ..i18n import _
 from ..node import (
@@ -449,3 +450,31 @@
             delta=delta)
 
         prevrev = rev
+
+def deltaiscensored(delta, baserev, baselenfn):
+    """Determine if a delta represents censored revision data.
+
+    ``baserev`` is the base revision this delta is encoded against.
+    ``baselenfn`` is a callable receiving a revision number that resolves the
+    length of the revision fulltext.
+
+    Returns a bool indicating if the result of the delta represents a censored
+    revision.
+    """
+    # Fragile heuristic: unless new file meta keys are added alphabetically
+    # preceding "censored", all censored revisions are prefixed by
+    # "\1\ncensored:". A delta producing such a censored revision must be a
+    # full-replacement delta, so we inspect the first and only patch in the
+    # delta for this prefix.
+    hlen = struct.calcsize(">lll")
+    if len(delta) <= hlen:
+        return False
+
+    oldlen = baselenfn(baserev)
+    newlen = len(delta) - hlen
+    if delta[:hlen] != mdiff.replacediffheader(oldlen, newlen):
+        return False
+
+    add = "\1\ncensored:"
+    addlen = len(add)
+    return newlen >= addlen and delta[hlen:hlen + addlen] == add