[PATCH 4 of 9 V6] bookmarks: introduce binary encoding
Stanislau Hlebik
stash at fb.com
Tue Oct 11 12:25:46 EDT 2016
# HG changeset patch
# User Stanislau Hlebik <stash at fb.com>
# Date 1476195835 25200
# Tue Oct 11 07:23:55 2016 -0700
# Node ID 718ed86a3698631077a087efaf668d70513056f5
# Parent 6f5a3300a5457c92eb09170a30c98328ebe3bcce
bookmarks: introduce binary encoding
Bookmarks binary encoding will be used for `bookmarks` bundle2 part.
The format is: <4 bytes - bookmark size, big-endian><bookmark>
<1 byte - 0 if node is empty 1 otherwise><20 bytes node>
BookmarksEncodeError and BookmarksDecodeError maybe thrown if input is
incorrect.
diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -7,8 +7,10 @@
from __future__ import absolute_import
+import StringIO
import errno
import os
+import struct
from .i18n import _
from .node import (
@@ -23,6 +25,77 @@
util,
)
+_NONEMPTYNODE = chr(1)
+_EMPTYNODE = chr(0)
+
+def _packbookmarksize(size):
+ return struct.pack('>i', size)
+
+def _unpackbookmarksize(stream):
+ """Returns 0 if stream is empty.
+ """
+
+ expectedsize = struct.calcsize('>i')
+ encodedbookmarksize = stream.read(expectedsize)
+ if len(encodedbookmarksize) == 0:
+ return 0
+ if len(encodedbookmarksize) != expectedsize:
+ raise error.BookmarksDecodeError(
+ _('cannot decode bookmark size: '
+ 'expected size: %d, actual size: %d') %
+ (expectedsize, len(encodedbookmarksize)))
+ return struct.unpack('>i', encodedbookmarksize)[0]
+
+def encodebookmarks(bookmarks):
+ """Encodes bookmark to node mapping to the byte string.
+
+ Format: <4 bytes - bookmark size, big-endian><bookmark>
+ <1 byte - 0 if node is empty 1 otherwise><20 bytes node>
+ Node may be 20 byte binary string, 40 byte hex string or empty
+ """
+
+ parts = []
+ for bookmark, node in bookmarks.iteritems():
+ encodedbookmarksize = _packbookmarksize(len(bookmark))
+ parts.append(encodedbookmarksize)
+ bookmark = encoding.fromlocal(bookmark)
+ parts.append(bookmark)
+ if node:
+ if len(node) != 20 and len(node) != 40:
+ raise error.BookmarksEncodeError()
+ if len(node) == 40:
+ node = bin(node)
+ parts.append(_NONEMPTYNODE)
+ parts.append(node)
+ else:
+ parts.append(_EMPTYNODE)
+ return ''.join(parts)
+
+def decodebookmarks(buf):
+ """Decodes buffer into bookmark to node mapping.
+
+ Node is either 20 bytes or empty.
+ """
+
+ stream = StringIO.StringIO(buf)
+ bookmarks = {}
+ while True:
+ bookmarksize = _unpackbookmarksize(stream)
+ if not bookmarksize:
+ break
+ bookmark = stream.read(bookmarksize)
+ if len(bookmark) != bookmarksize:
+ raise error.BookmarksDecodeError(
+ 'cannot decode bookmark: expected size: %d, '
+ 'actual size: %d' % (bookmarksize, len(bookmark)))
+ bookmark = encoding.tolocal(bookmark)
+ emptynode = stream.read(1)
+ node = ''
+ if emptynode != _EMPTYNODE:
+ node = stream.read(20)
+ bookmarks[bookmark] = node
+ return bookmarks
+
def _getbkfile(repo):
"""Hook so that extensions that mess with the store can hook bm storage.
More information about the Mercurial-devel
mailing list