Patchwork D10774: revlogv2: add a `get_data` helper to grab the next piece of docket

login
register
mail settings
Submitter phabricator
Date May 27, 2021, 7:05 a.m.
Message ID <differential-rev-PHID-DREV-caqbmuwbyexawwdp5nb4-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49093/
State Superseded
Headers show

Comments

phabricator - May 27, 2021, 7:05 a.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This make the processing more compact but abstracting repetitive processing
  away.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/revlogutils/docket.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/revlogutils/docket.py b/mercurial/revlogutils/docket.py
--- a/mercurial/revlogutils/docket.py
+++ b/mercurial/revlogutils/docket.py
@@ -237,19 +237,30 @@ 
 def parse_docket(revlog, data, use_pending=False):
     """given some docket data return a docket object for the given revlog"""
     header = S_HEADER.unpack(data[: S_HEADER.size])
-    offset = S_HEADER.size
+
+    # this is a mutable closure capture used in `get_data`
+    offset = [S_HEADER.size]
+
+    def get_data(size):
+        """utility closure to access the `size` next bytes"""
+        if offset[0] + size > len(data):
+            # XXX better class
+            msg = b"docket is too short, expected %d got %d"
+            msg %= (offset[0] + size, len(data))
+            raise error.Abort(msg)
+        raw = data[offset[0] : offset[0] + size]
+        offset[0] += size
+        return raw
 
     iheader = iter(header)
 
     version_header = next(iheader)
 
     index_uuid_size = next(iheader)
-    index_uuid = data[offset : offset + index_uuid_size]
-    offset += index_uuid_size
+    index_uuid = get_data(index_uuid_size)
 
     data_uuid_size = next(iheader)
-    data_uuid = data[offset : offset + data_uuid_size]
-    offset += data_uuid_size
+    data_uuid = get_data(data_uuid_size)
 
     index_size = next(iheader)