changeset 47242:4d1c893b9095

revlog: unify flag processing when loading index The new code use a simple declaration to do centralised processing. This is clearer, shorter and less error prone. This will be especially useful as we plan to add a fourth format: changelog-v2. Differential Revision: https://phab.mercurial-scm.org/D10622
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 03 May 2021 12:30:46 +0200
parents 0e9105bf54cb
children 33096e77598c
files mercurial/revlog.py mercurial/revlogutils/constants.py
diffstat 2 files changed, 35 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revlog.py	Mon May 03 12:30:35 2021 +0200
+++ b/mercurial/revlog.py	Mon May 03 12:30:46 2021 +0200
@@ -35,6 +35,7 @@
 from .pycompat import getattr
 from .revlogutils.constants import (
     ALL_KINDS,
+    FEATURES_BY_VERSION,
     FLAG_GENERALDELTA,
     FLAG_INLINE_DATA,
     INDEX_HEADER,
@@ -499,24 +500,10 @@
             msg %= (display_flag, self._format_version, self.display_id)
             raise error.RevlogError(msg)
 
-        if self._format_version == REVLOGV0:
-            self._inline = False
-            self._generaldelta = False
-        elif self._format_version == REVLOGV1:
-            self._inline = self._format_flags & FLAG_INLINE_DATA
-            self._generaldelta = self._format_flags & FLAG_GENERALDELTA
-        elif self._format_version == REVLOGV2:
-            # There is a bug in the transaction handling when going from an
-            # inline revlog to a separate index and data file. Turn it off until
-            # it's fixed, since v2 revlogs sometimes get rewritten on exchange.
-            # See issue6485
-            self._inline = False
-            # generaldelta implied by version 2 revlogs.
-            self._generaldelta = True
-            # revlog-v2 has built in sidedata support
-            self.hassidedata = True
-        else:
-            assert False, 'unreachable'
+        features = FEATURES_BY_VERSION[self._format_version]
+        self._inline = features[b'inline'](self._format_flags)
+        self._generaldelta = features[b'generaldelta'](self._format_flags)
+        self.hassidedata = features[b'sidedata']
 
         index_data = entry_data
         self._indexfile = entry_point
--- a/mercurial/revlogutils/constants.py	Mon May 03 12:30:35 2021 +0200
+++ b/mercurial/revlogutils/constants.py	Mon May 03 12:30:46 2021 +0200
@@ -120,4 +120,34 @@
     REVLOGV2: REVLOGV2_FLAGS,
 }
 
+_no = lambda flags: False
+_yes = lambda flags: True
+
+
+def _from_flag(flag):
+    return lambda flags: bool(flags & flag)
+
+
+FEATURES_BY_VERSION = {
+    REVLOGV0: {
+        b'inline': _no,
+        b'generaldelta': _no,
+        b'sidedata': False,
+    },
+    REVLOGV1: {
+        b'inline': _from_flag(FLAG_INLINE_DATA),
+        b'generaldelta': _from_flag(FLAG_GENERALDELTA),
+        b'sidedata': False,
+    },
+    REVLOGV2: {
+        # There is a bug in the transaction handling when going from an
+        # inline revlog to a separate index and data file. Turn it off until
+        # it's fixed, since v2 revlogs sometimes get rewritten on exchange.
+        # See issue6485
+        b'inline': _no,
+        b'generaldelta': _yes,
+        b'sidedata': True,
+    },
+}
+
 SPARSE_REVLOG_MAX_CHAIN_LENGTH = 1000