revlog: unify checks for supported flag
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 03 May 2021 12:30:35 +0200
changeset 47230 0e9105bf54cb
parent 47229 21b3e6116bd1
child 47231 4d1c893b9095
revlog: unify checks for supported flag The new code use a simple declaration to do centralised checking. 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/D10621
mercurial/revlog.py
mercurial/revlogutils/constants.py
--- a/mercurial/revlog.py	Mon May 03 12:30:24 2021 +0200
+++ b/mercurial/revlog.py	Mon May 03 12:30:35 2021 +0200
@@ -46,6 +46,7 @@
     REVLOG_DEFAULT_FLAGS,
     REVLOG_DEFAULT_FORMAT,
     REVLOG_DEFAULT_VERSION,
+    SUPPORTED_FLAGS,
 )
 from .revlogutils.flagutil import (
     REVIDX_DEFAULT_FLAGS,
@@ -487,33 +488,24 @@
         self._format_flags = header & ~0xFFFF
         self._format_version = header & 0xFFFF
 
+        supported_flags = SUPPORTED_FLAGS.get(self._format_version)
+        if supported_flags is None:
+            msg = _(b'unknown version (%d) in revlog %s')
+            msg %= (self._format_version, self.display_id)
+            raise error.RevlogError(msg)
+        elif self._format_flags & ~supported_flags:
+            msg = _(b'unknown flags (%#04x) in version %d revlog %s')
+            display_flag = self._format_flags >> 16
+            msg %= (display_flag, self._format_version, self.display_id)
+            raise error.RevlogError(msg)
+
         if self._format_version == REVLOGV0:
-            if self._format_flags:
-                msg = _(b'unknown flags (%#04x) in version %d revlog %s')
-                display_flag = self._format_flags >> 16
-                msg %= (display_flag, self._format_version, self.display_id)
-                raise error.RevlogError(msg)
-
             self._inline = False
             self._generaldelta = False
-
         elif self._format_version == REVLOGV1:
-            if self._format_flags & ~REVLOGV1_FLAGS:
-                msg = _(b'unknown flags (%#04x) in version %d revlog %s')
-                display_flag = self._format_flags >> 16
-                msg %= (display_flag, self._format_version, self.display_id)
-                raise error.RevlogError(msg)
-
             self._inline = self._format_flags & FLAG_INLINE_DATA
             self._generaldelta = self._format_flags & FLAG_GENERALDELTA
-
         elif self._format_version == REVLOGV2:
-            if self._format_flags & ~REVLOGV2_FLAGS:
-                msg = _(b'unknown flags (%#04x) in version %d revlog %s')
-                display_flag = self._format_flags >> 16
-                msg %= (display_flag, self._format_version, self.display_id)
-                raise error.RevlogError(msg)
-
             # 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.
@@ -523,11 +515,8 @@
             self._generaldelta = True
             # revlog-v2 has built in sidedata support
             self.hassidedata = True
-
         else:
-            msg = _(b'unknown version (%d) in revlog %s')
-            msg %= (self._format_version, self.display_id)
-            raise error.RevlogError(msg)
+            assert False, 'unreachable'
 
         index_data = entry_data
         self._indexfile = entry_point
--- a/mercurial/revlogutils/constants.py	Mon May 03 12:30:24 2021 +0200
+++ b/mercurial/revlogutils/constants.py	Mon May 03 12:30:35 2021 +0200
@@ -45,6 +45,7 @@
 REVLOG_DEFAULT_FLAGS = FLAG_INLINE_DATA
 REVLOG_DEFAULT_FORMAT = REVLOGV1
 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
+REVLOGV0_FLAGS = 0
 REVLOGV1_FLAGS = FLAG_INLINE_DATA | FLAG_GENERALDELTA
 REVLOGV2_FLAGS = FLAG_INLINE_DATA
 
@@ -113,4 +114,10 @@
 # bitmark for flags that could cause rawdata content change
 REVIDX_RAWTEXT_CHANGING_FLAGS = REVIDX_ISCENSORED | REVIDX_EXTSTORED
 
+SUPPORTED_FLAGS = {
+    REVLOGV0: REVLOGV0_FLAGS,
+    REVLOGV1: REVLOGV1_FLAGS,
+    REVLOGV2: REVLOGV2_FLAGS,
+}
+
 SPARSE_REVLOG_MAX_CHAIN_LENGTH = 1000