diff mercurial/revlog.py @ 37442:0596d27457c6

revlog: move parsemeta() and packmeta() from filelog (API) filelog.parsemeta() and filelog.packmeta() are used to decode and encode metadata for file copies and censor. An upcoming commit will move the core logic for censoring revlogs into revlog.py. This would create a cycle between revlog.py and filelog.py. So we move these metadata functions to revlog.py. .. api:: filelog.parsemeta() and filelog.packmeta() have been moved to the revlog module. Differential Revision: https://phab.mercurial-scm.org/D3150
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 05 Apr 2018 18:22:35 -0700
parents f0b6fbea00cf
children 65250a66b55c
line wrap: on
line diff
--- a/mercurial/revlog.py	Thu Apr 05 15:18:23 2018 -0700
+++ b/mercurial/revlog.py	Thu Apr 05 18:22:35 2018 -0700
@@ -19,6 +19,7 @@
 import hashlib
 import heapq
 import os
+import re
 import struct
 import zlib
 
@@ -97,6 +98,25 @@
     REVIDX_ISCENSORED: None,
 }
 
+_mdre = re.compile('\1\n')
+def parsemeta(text):
+    """return (metadatadict, metadatasize)"""
+    # text can be buffer, so we can't use .startswith or .index
+    if text[:2] != '\1\n':
+        return None, None
+    s = _mdre.search(text, 2).start()
+    mtext = text[2:s]
+    meta = {}
+    for l in mtext.splitlines():
+        k, v = l.split(": ", 1)
+        meta[k] = v
+    return meta, (s + 2)
+
+def packmeta(meta, text):
+    keys = sorted(meta)
+    metatext = "".join("%s: %s\n" % (k, meta[k]) for k in keys)
+    return "\1\n%s\1\n%s" % (metatext, text)
+
 def addflagprocessor(flag, processor):
     """Register a flag processor on a revision data flag.