Mercurial > public > mercurial-scm > hg-stable
diff mercurial/utils/storageutil.py @ 39883:3e896b51aa5d
storageutil: move metadata parsing and packing from revlog (API)
Parsing and writing of revision text metadata is likely identical
across storage backends. Let's move the code out of revlog so we
don't need to import the revlog module in order to use it.
Differential Revision: https://phab.mercurial-scm.org/D4754
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 24 Sep 2018 14:31:31 -0700 |
parents | f8eb71f9e3bd |
children | d269ddbf54f0 |
line wrap: on
line diff
--- a/mercurial/utils/storageutil.py Mon Sep 24 14:23:54 2018 -0700 +++ b/mercurial/utils/storageutil.py Mon Sep 24 14:31:31 2018 -0700 @@ -8,6 +8,7 @@ from __future__ import absolute_import import hashlib +import re from ..node import ( nullid, @@ -39,3 +40,28 @@ s.update(b) s.update(text) return s.digest() + +METADATA_RE = re.compile(b'\x01\n') + +def parsemeta(text): + """Parse metadata header from revision data. + + Returns a 2-tuple of (metadata, offset), where both can be None if there + is no metadata. + """ + # text can be buffer, so we can't use .startswith or .index + if text[:2] != b'\x01\n': + return None, None + s = METADATA_RE.search(text, 2).start() + mtext = text[2:s] + meta = {} + for l in mtext.splitlines(): + k, v = l.split(b': ', 1) + meta[k] = v + return meta, s + 2 + +def packmeta(meta, text): + """Add metadata to fulltext to produce revision text.""" + keys = sorted(meta) + metatext = b''.join(b'%s: %s\n' % (k, meta[k]) for k in keys) + return b'\x01\n%s\x01\n%s' % (metatext, text)