Patchwork D10621: revlog: unify checks for supported flag

login
register
mail settings
Submitter phabricator
Date May 3, 2021, 12:08 p.m.
Message ID <differential-rev-PHID-DREV-ffbe4nir22vuythfwcff-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48938/
State New
Headers show

Comments

phabricator - May 3, 2021, 12: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
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

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

CHANGE DETAILS




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

Patch

diff --git a/mercurial/revlogutils/constants.py b/mercurial/revlogutils/constants.py
--- a/mercurial/revlogutils/constants.py
+++ b/mercurial/revlogutils/constants.py
@@ -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
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -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