Patchwork [2,of,7,bundle2] phases: use a Struct object for binary encoding and decoding

login
register
mail settings
Submitter Boris Feld
Date Sept. 28, 2017, 5:08 a.m.
Message ID <1efdcfffd51b31fad818.1506575317@FB>
Download mbox | patch
Permalink /patch/24195/
State Accepted
Headers show

Comments

Boris Feld - Sept. 28, 2017, 5:08 a.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1505851689 -7200
#      Tue Sep 19 22:08:09 2017 +0200
# Node ID 1efdcfffd51b31fad818b2c772a2a8883da3de2b
# Parent  5458212fddf54bad3e7590e2ea2600073597044e
# EXP-Topic b2.phases
phases: use a Struct object for binary encoding and decoding

We will move the binary encoding and decoding code to 'phases.py' in order to
make it easier to reuse. First, let's cleanup it a bit.

Patch

diff -r 5458212fddf5 -r 1efdcfffd51b mercurial/bundle2.py
--- a/mercurial/bundle2.py	Wed Sep 20 05:47:33 2017 +0200
+++ b/mercurial/bundle2.py	Tue Sep 19 22:08:09 2017 +0200
@@ -179,7 +179,7 @@ 
 _fpayloadsize = '>i'
 _fpartparamcount = '>BB'
 
-_fphasesentry = '>i20s'
+_fphasesentry = struct.Struct('>i20s')
 
 preferedchunksize = 4096
 
@@ -1483,7 +1483,7 @@ 
         phasedata = []
         for phase in phases.allphases:
             for head in headsbyphase[phase]:
-                phasedata.append(_pack(_fphasesentry, phase, head))
+                phasedata.append(_fphasesentry.pack(phase, head))
         bundler.newpart('phase-heads', data=''.join(phasedata))
 
 def addparttagsfnodescache(repo, bundler, outgoing):
@@ -1843,14 +1843,14 @@ 
 
 def _readphaseheads(inpart):
     headsbyphase = [[] for i in phases.allphases]
-    entrysize = struct.calcsize(_fphasesentry)
+    entrysize = _fphasesentry.size
     while True:
         entry = inpart.read(entrysize)
         if len(entry) < entrysize:
             if entry:
                 raise error.Abort(_('bad phase-heads bundle part'))
             break
-        phase, node = struct.unpack(_fphasesentry, entry)
+        phase, node = _fphasesentry.unpack(entry)
         headsbyphase[phase].append(node)
     return headsbyphase