revlogv2: add a `get_data` helper to grab the next piece of docket
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 20 May 2021 21:54:21 +0200
changeset 47326 53ab13d6a5db
parent 47325 f286d715f9ab
child 47327 1844a2e3401c
revlogv2: add a `get_data` helper to grab the next piece of docket This make the processing more compact but abstracting repetitive processing away. Differential Revision: https://phab.mercurial-scm.org/D10774
mercurial/revlogutils/docket.py
--- a/mercurial/revlogutils/docket.py	Thu May 20 21:48:53 2021 +0200
+++ b/mercurial/revlogutils/docket.py	Thu May 20 21:54:21 2021 +0200
@@ -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)