comparison 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
comparison
equal deleted inserted replaced
39882:f8eb71f9e3bd 39883:3e896b51aa5d
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 import hashlib 10 import hashlib
11 import re
11 12
12 from ..node import ( 13 from ..node import (
13 nullid, 14 nullid,
14 ) 15 )
15 16
37 b = p1 38 b = p1
38 s = hashlib.sha1(a) 39 s = hashlib.sha1(a)
39 s.update(b) 40 s.update(b)
40 s.update(text) 41 s.update(text)
41 return s.digest() 42 return s.digest()
43
44 METADATA_RE = re.compile(b'\x01\n')
45
46 def parsemeta(text):
47 """Parse metadata header from revision data.
48
49 Returns a 2-tuple of (metadata, offset), where both can be None if there
50 is no metadata.
51 """
52 # text can be buffer, so we can't use .startswith or .index
53 if text[:2] != b'\x01\n':
54 return None, None
55 s = METADATA_RE.search(text, 2).start()
56 mtext = text[2:s]
57 meta = {}
58 for l in mtext.splitlines():
59 k, v = l.split(b': ', 1)
60 meta[k] = v
61 return meta, s + 2
62
63 def packmeta(meta, text):
64 """Add metadata to fulltext to produce revision text."""
65 keys = sorted(meta)
66 metatext = b''.join(b'%s: %s\n' % (k, meta[k]) for k in keys)
67 return b'\x01\n%s\x01\n%s' % (metatext, text)