Patchwork D10511: revlog: code for `revlogv0` in its own module

login
register
mail settings
Submitter phabricator
Date April 28, 2021, 3:08 p.m.
Message ID <differential-rev-PHID-DREV-rdcz4qpd65cc55pliyny-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48831/
State Superseded
Headers show

Comments

phabricator - April 28, 2021, 3:08 p.m.
marmoute created this revision.
Herald added a reviewer: indygreg.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This code is mostly unused compatiblity code. Yet it take a prohiminent place in the `revlog.py` module. That module is already quite big, so we move all that code in a dedicated module.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10511

AFFECTED FILES
  mercurial/revlog.py
  mercurial/revlogutils/revlogv0.py

CHANGE DETAILS




To: marmoute, indygreg, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/revlogutils/revlogv0.py b/mercurial/revlogutils/revlogv0.py
new file mode 100644
--- /dev/null
+++ b/mercurial/revlogutils/revlogv0.py
@@ -0,0 +1,144 @@ 
+# revlogv0 - code related to revlog format "V0"
+#
+# Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+from __future__ import absolute_import
+
+
+from ..node import sha1nodeconstants
+from .constants import (
+    INDEX_ENTRY_V0,
+)
+from ..i18n import _
+
+from .. import (
+    error,
+    node,
+    pycompat,
+    util,
+)
+
+from . import (
+    flagutil,
+    nodemap as nodemaputil,
+)
+
+
+def getoffset(q):
+    return int(q >> 16)
+
+
+def gettype(q):
+    return int(q & 0xFFFF)
+
+
+def offset_type(offset, type):
+    if (type & ~flagutil.REVIDX_KNOWN_FLAGS) != 0:
+        raise ValueError(b'unknown revlog index flags')
+    return int(int(offset) << 16 | type)
+
+
+class revlogoldindex(list):
+    entry_size = INDEX_ENTRY_V0.size
+
+    @property
+    def nodemap(self):
+        msg = b"index.nodemap is deprecated, use index.[has_node|rev|get_rev]"
+        util.nouideprecwarn(msg, b'5.3', stacklevel=2)
+        return self._nodemap
+
+    @util.propertycache
+    def _nodemap(self):
+        nodemap = nodemaputil.NodeMap({sha1nodeconstants.nullid: node.nullrev})
+        for r in range(0, len(self)):
+            n = self[r][7]
+            nodemap[n] = r
+        return nodemap
+
+    def has_node(self, node):
+        """return True if the node exist in the index"""
+        return node in self._nodemap
+
+    def rev(self, node):
+        """return a revision for a node
+
+        If the node is unknown, raise a RevlogError"""
+        return self._nodemap[node]
+
+    def get_rev(self, node):
+        """return a revision for a node
+
+        If the node is unknown, return None"""
+        return self._nodemap.get(node)
+
+    def append(self, tup):
+        self._nodemap[tup[7]] = len(self)
+        super(revlogoldindex, self).append(tup)
+
+    def __delitem__(self, i):
+        if not isinstance(i, slice) or not i.stop == -1 or i.step is not None:
+            raise ValueError(b"deleting slices only supports a:-1 with step 1")
+        for r in pycompat.xrange(i.start, len(self)):
+            del self._nodemap[self[r][7]]
+        super(revlogoldindex, self).__delitem__(i)
+
+    def clearcaches(self):
+        self.__dict__.pop('_nodemap', None)
+
+    def __getitem__(self, i):
+        if i == -1:
+            return (0, 0, 0, -1, -1, -1, -1, node.nullid)
+        return list.__getitem__(self, i)
+
+    def pack_header(self, header):
+        """pack header information in binary"""
+        return b''
+
+    def entry_binary(self, rev):
+        """return the raw binary string representing a revision"""
+        entry = self[rev]
+        if gettype(entry[0]):
+            raise error.RevlogError(
+                _(b'index entry flags need revlog version 1')
+            )
+        e2 = (
+            getoffset(entry[0]),
+            entry[1],
+            entry[3],
+            entry[4],
+            self[entry[5]][7],
+            self[entry[6]][7],
+            entry[7],
+        )
+        return INDEX_ENTRY_V0.pack(*e2)
+
+
+def parse_index_v0(data, inline):
+    s = INDEX_ENTRY_V0.size
+    index = []
+    nodemap = nodemaputil.NodeMap({node.nullid: node.nullrev})
+    n = off = 0
+    l = len(data)
+    while off + s <= l:
+        cur = data[off : off + s]
+        off += s
+        e = INDEX_ENTRY_V0.unpack(cur)
+        # transform to revlogv1 format
+        e2 = (
+            offset_type(e[0], 0),
+            e[1],
+            -1,
+            e[2],
+            e[3],
+            nodemap.get(e[4], node.nullrev),
+            nodemap.get(e[5], node.nullrev),
+            e[6],
+        )
+        index.append(e2)
+        nodemap[e[6]] = n
+        n += 1
+
+    index = revlogoldindex(index)
+    return index, None
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -36,7 +36,6 @@ 
 from .revlogutils.constants import (
     FLAG_GENERALDELTA,
     FLAG_INLINE_DATA,
-    INDEX_ENTRY_V0,
     INDEX_HEADER,
     REVLOGV0,
     REVLOGV1,
@@ -76,6 +75,7 @@ 
     deltas as deltautil,
     flagutil,
     nodemap as nodemaputil,
+    revlogv0,
     sidedata as sidedatautil,
 )
 from .utils import (
@@ -136,14 +136,6 @@ 
 )
 
 
-def getoffset(q):
-    return int(q >> 16)
-
-
-def gettype(q):
-    return int(q & 0xFFFF)
-
-
 def offset_type(offset, type):
     if (type & ~flagutil.REVIDX_KNOWN_FLAGS) != 0:
         raise ValueError(b'unknown revlog index flags')
@@ -214,110 +206,6 @@ 
     node = attr.ib(default=None)
 
 
-class revlogoldindex(list):
-    entry_size = INDEX_ENTRY_V0.size
-
-    @property
-    def nodemap(self):
-        msg = b"index.nodemap is deprecated, use index.[has_node|rev|get_rev]"
-        util.nouideprecwarn(msg, b'5.3', stacklevel=2)
-        return self._nodemap
-
-    @util.propertycache
-    def _nodemap(self):
-        nodemap = nodemaputil.NodeMap({sha1nodeconstants.nullid: nullrev})
-        for r in range(0, len(self)):
-            n = self[r][7]
-            nodemap[n] = r
-        return nodemap
-
-    def has_node(self, node):
-        """return True if the node exist in the index"""
-        return node in self._nodemap
-
-    def rev(self, node):
-        """return a revision for a node
-
-        If the node is unknown, raise a RevlogError"""
-        return self._nodemap[node]
-
-    def get_rev(self, node):
-        """return a revision for a node
-
-        If the node is unknown, return None"""
-        return self._nodemap.get(node)
-
-    def append(self, tup):
-        self._nodemap[tup[7]] = len(self)
-        super(revlogoldindex, self).append(tup)
-
-    def __delitem__(self, i):
-        if not isinstance(i, slice) or not i.stop == -1 or i.step is not None:
-            raise ValueError(b"deleting slices only supports a:-1 with step 1")
-        for r in pycompat.xrange(i.start, len(self)):
-            del self._nodemap[self[r][7]]
-        super(revlogoldindex, self).__delitem__(i)
-
-    def clearcaches(self):
-        self.__dict__.pop('_nodemap', None)
-
-    def __getitem__(self, i):
-        if i == -1:
-            return (0, 0, 0, -1, -1, -1, -1, sha1nodeconstants.nullid)
-        return list.__getitem__(self, i)
-
-    def entry_binary(self, rev):
-        """return the raw binary string representing a revision"""
-        entry = self[rev]
-        if gettype(entry[0]):
-            raise error.RevlogError(
-                _(b'index entry flags need revlog version 1')
-            )
-        e2 = (
-            getoffset(entry[0]),
-            entry[1],
-            entry[3],
-            entry[4],
-            self[entry[5]][7],
-            self[entry[6]][7],
-            entry[7],
-        )
-        return INDEX_ENTRY_V0.pack(*e2)
-
-    def pack_header(self, header):
-        """Pack header information in binary"""
-        return b''
-
-
-def parse_index_v0(data, inline):
-    s = INDEX_ENTRY_V0.size
-    index = []
-    nodemap = nodemaputil.NodeMap({sha1nodeconstants.nullid: nullrev})
-    n = off = 0
-    l = len(data)
-    while off + s <= l:
-        cur = data[off : off + s]
-        off += s
-        e = INDEX_ENTRY_V0.unpack(cur)
-        # transform to revlogv1 format
-        e2 = (
-            offset_type(e[0], 0),
-            e[1],
-            -1,
-            e[2],
-            e[3],
-            nodemap.get(e[4], nullrev),
-            nodemap.get(e[5], nullrev),
-            e[6],
-        )
-        index.append(e2)
-        nodemap[e[6]] = n
-        n += 1
-
-    index = revlogoldindex(index)
-    return index, None
-
-
 def parse_index_v1(data, inline):
     # call the C implementation to parse the index data
     index, cache = parsers.parse_index2(data, inline)
@@ -621,7 +509,7 @@ 
 
         self._parse_index = parse_index_v1
         if self.version == REVLOGV0:
-            self._parse_index = parse_index_v0
+            self._parse_index = revlogv0.parse_index_v0
         elif fmt == REVLOGV2:
             self._parse_index = parse_index_v2
         elif devel_nodemap: