revlog: move parsemeta() and packmeta() from filelog (API)
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 05 Apr 2018 18:22:35 -0700
changeset 37442 0596d27457c6
parent 37441 a3202fa83aff
child 37443 65250a66b55c
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
hgext/censor.py
hgext/lfs/wrapper.py
mercurial/filelog.py
mercurial/revlog.py
tests/simplestorerepo.py
--- a/hgext/censor.py	Thu Apr 05 15:18:23 2018 -0700
+++ b/hgext/censor.py	Thu Apr 05 18:22:35 2018 -0700
@@ -32,7 +32,6 @@
 
 from mercurial import (
     error,
-    filelog,
     lock as lockmod,
     registrar,
     revlog,
@@ -106,7 +105,7 @@
         raise error.Abort(
             _('censor does not support revlog version %d') % (flogv,))
 
-    tombstone = filelog.packmeta({"censored": tombstone}, "")
+    tombstone = revlog.packmeta({"censored": tombstone}, "")
 
     crev = fctx.filerev()
 
--- a/hgext/lfs/wrapper.py	Thu Apr 05 15:18:23 2018 -0700
+++ b/hgext/lfs/wrapper.py	Thu Apr 05 18:22:35 2018 -0700
@@ -14,7 +14,6 @@
 
 from mercurial import (
     error,
-    filelog,
     revlog,
     util,
 )
@@ -69,13 +68,13 @@
             name = k[len('x-hg-'):]
             hgmeta[name] = p[k]
     if hgmeta or text.startswith('\1\n'):
-        text = filelog.packmeta(hgmeta, text)
+        text = revlog.packmeta(hgmeta, text)
 
     return (text, True)
 
 def writetostore(self, text):
     # hg filelog metadata (includes rename, etc)
-    hgmeta, offset = filelog.parsemeta(text)
+    hgmeta, offset = revlog.parsemeta(text)
     if offset and offset > 0:
         # lfs blob does not contain hg filelog metadata
         text = text[offset:]
@@ -121,7 +120,7 @@
                        flags=revlog.REVIDX_DEFAULT_FLAGS, **kwds):
     textlen = len(text)
     # exclude hg rename meta from file size
-    meta, offset = filelog.parsemeta(text)
+    meta, offset = revlog.parsemeta(text)
     if offset:
         textlen -= offset
 
--- a/mercurial/filelog.py	Thu Apr 05 15:18:23 2018 -0700
+++ b/mercurial/filelog.py	Thu Apr 05 18:22:35 2018 -0700
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import re
 import struct
 
 from .thirdparty.zope import (
@@ -20,27 +19,8 @@
     revlog,
 )
 
-_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 _censoredtext(text):
-    m, offs = parsemeta(text)
+    m, offs = revlog.parsemeta(text)
     return m and "censored" in m
 
 @zi.implementer(repository.ifilestorage)
@@ -60,14 +40,14 @@
 
     def add(self, text, meta, transaction, link, p1=None, p2=None):
         if meta or text.startswith('\1\n'):
-            text = packmeta(meta, text)
+            text = revlog.packmeta(meta, text)
         return self.addrevision(text, transaction, link, p1, p2)
 
     def renamed(self, node):
         if self.parents(node)[0] != revlog.nullid:
             return False
         t = self.revision(node)
-        m = parsemeta(t)[0]
+        m = revlog.parsemeta(t)[0]
         if m and "copy" in m:
             return (m["copy"], revlog.bin(m["copyrev"]))
         return False
--- 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.
 
--- a/tests/simplestorerepo.py	Thu Apr 05 15:18:23 2018 -0700
+++ b/tests/simplestorerepo.py	Thu Apr 05 18:22:35 2018 -0700
@@ -32,7 +32,6 @@
     bundlerepo,
     error,
     extensions,
-    filelog,
     localrepo,
     mdiff,
     pycompat,
@@ -326,7 +325,7 @@
             return False
 
         fulltext = self.revision(node)
-        m = filelog.parsemeta(fulltext)[0]
+        m = revlog.parsemeta(fulltext)[0]
 
         if m and 'copy' in m:
             return m['copy'], bin(m['copyrev'])
@@ -415,7 +414,7 @@
 
     def add(self, text, meta, transaction, linkrev, p1, p2):
         if meta or text.startswith(b'\1\n'):
-            text = filelog.packmeta(meta, text)
+            text = revlog.packmeta(meta, text)
 
         return self.addrevision(text, transaction, linkrev, p1, p2)