Patchwork [3,of,8] revlog: extract 'buildtext' closure function from _addrevision

login
register
mail settings
Submitter Paul Morelle
Date Jan. 14, 2018, 10:28 a.m.
Message ID <2f39856d4feee57695b0.1515925699@taranis.localdomain>
Download mbox | patch
Permalink /patch/26735/
State Accepted
Headers show

Comments

Paul Morelle - Jan. 14, 2018, 10:28 a.m.
# HG changeset patch
# User Paul Morelle <paul.morelle@octobus.net>
# Date 1515768925 -3600
#      Fri Jan 12 15:55:25 2018 +0100
# Node ID 2f39856d4feee57695b05c9298a3bf1789edf173
# Parent  84eb864137a7b27e2357eb4f6d465f726670dc98
# EXP-Topic refactor-revlog
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 2f39856d4fee
revlog: extract 'buildtext' closure function from _addrevision

Patch

diff -r 84eb864137a7 -r 2f39856d4fee mercurial/revlog.py
--- a/mercurial/revlog.py	Fri Jan 12 16:42:55 2018 +0100
+++ b/mercurial/revlog.py	Fri Jan 12 15:55:25 2018 +0100
@@ -1882,6 +1882,47 @@ 
                 # fulltext.
                 yield (prev,)
 
+    def _buildtext(self, node, p1, p2, btext, cachedelta, fh, flags):
+        """Builds a fulltext version of a revision
+
+        node:       expected hash of the revision
+        p1, p2:     parent revs of the revision
+        btext:      built text cache consisting of a one-element list
+        cachedelta: (baserev, uncompressed_delta) or None
+        fh:         file handle to either the .i or the .d revlog file,
+                    depending on whether it is inlined or not
+        flags:      flags associated to the revision storage
+
+        One of btext[0] or cachedelta must be set.
+        """
+        if btext[0] is not None:
+            return btext[0]
+        baserev = cachedelta[0]
+        delta = cachedelta[1]
+        # special case deltas which replace entire base; no need to decode
+        # base revision. this neatly avoids censored bases, which throw when
+        # they're decoded.
+        hlen = struct.calcsize(">lll")
+        if delta[:hlen] == mdiff.replacediffheader(self.rawsize(baserev),
+                                                   len(delta) - hlen):
+            btext[0] = delta[hlen:]
+        else:
+            basetext = self.revision(baserev, _df=fh, raw=True)
+            btext[0] = mdiff.patch(basetext, delta)
+
+        try:
+            res = self._processflags(btext[0], flags, 'read', raw=True)
+            btext[0], validatehash = res
+            if validatehash:
+                self.checkhash(btext[0], node, p1=p1, p2=p2)
+            if flags & REVIDX_ISCENSORED:
+                raise RevlogError(_('node %s is not censored') % node)
+        except CensoredNodeError:
+            # must pass the censored index flag to add censored revisions
+            if not flags & REVIDX_ISCENSORED:
+                raise
+        return btext[0]
+
     def _addrevision(self, node, rawtext, transaction, link, p1, p2, flags,
                      cachedelta, ifh, dfh, alwayscache=False):
         """internal function to add revisions to the log
@@ -1907,41 +1948,13 @@ 
             fh = dfh
 
         btext = [rawtext]
-        def buildtext():
-            if btext[0] is not None:
-                return btext[0]
-            baserev = cachedelta[0]
-            delta = cachedelta[1]
-            # special case deltas which replace entire base; no need to decode
-            # base revision. this neatly avoids censored bases, which throw when
-            # they're decoded.
-            hlen = struct.calcsize(">lll")
-            if delta[:hlen] == mdiff.replacediffheader(self.rawsize(baserev),
-                                                       len(delta) - hlen):
-                btext[0] = delta[hlen:]
-            else:
-                basetext = self.revision(baserev, _df=fh, raw=True)
-                btext[0] = mdiff.patch(basetext, delta)
-
-            try:
-                res = self._processflags(btext[0], flags, 'read', raw=True)
-                btext[0], validatehash = res
-                if validatehash:
-                    self.checkhash(btext[0], node, p1=p1, p2=p2)
-                if flags & REVIDX_ISCENSORED:
-                    raise RevlogError(_('node %s is not censored') % node)
-            except CensoredNodeError:
-                # must pass the censored index flag to add censored revisions
-                if not flags & REVIDX_ISCENSORED:
-                    raise
-            return btext[0]
 
         def builddelta(rev):
             # can we use the cached delta?
             if cachedelta and cachedelta[0] == rev:
                 delta = cachedelta[1]
             else:
-                t = buildtext()
+                t = self._buildtext(node, p1, p2, btext, cachedelta, fh, flags)
                 if self.iscensored(rev):
                     # deltas based on a censored revision must replace the
                     # full content in one patch, so delta works everywhere
@@ -1991,7 +2004,8 @@ 
         if delta is not None:
             dist, l, data, base, chainbase, chainlen, compresseddeltalen = delta
         else:
-            rawtext = buildtext()
+            rawtext = self._buildtext(node, p1, p2, btext, cachedelta, fh,
+                                      flags)
             data = self.compress(rawtext)
             l = len(data[1]) + len(data[0])
             base = chainbase = curr
@@ -2005,7 +2019,8 @@ 
         self._writeentry(transaction, ifh, dfh, entry, data, link, offset)
 
         if alwayscache and rawtext is None:
-            rawtext = buildtext()
+            rawtext = self._buildtext(node, p1, p2, btext, cachedelta, fh,
+                                      flags)
 
         if type(rawtext) == str: # only accept immutable objects
             self._cache = (node, curr, rawtext)