Patchwork D3150: revlog: move parsemeta() and packmeta() from filelog (API)

login
register
mail settings
Submitter phabricator
Date April 6, 2018, 12:51 a.m.
Message ID <differential-rev-PHID-DREV-lcupd32n3mbiuc23yrnd-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/30417/
State Superseded
Headers show

Comments

phabricator - April 6, 2018, 12:51 a.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3150

AFFECTED FILES
  hgext/censor.py
  hgext/lfs/wrapper.py
  mercurial/filelog.py
  mercurial/revlog.py
  tests/simplestorerepo.py

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/tests/simplestorerepo.py b/tests/simplestorerepo.py
--- a/tests/simplestorerepo.py
+++ b/tests/simplestorerepo.py
@@ -326,7 +326,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 +415,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)
 
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -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.
 
diff --git a/mercurial/filelog.py b/mercurial/filelog.py
--- a/mercurial/filelog.py
+++ b/mercurial/filelog.py
@@ -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
diff --git a/hgext/lfs/wrapper.py b/hgext/lfs/wrapper.py
--- a/hgext/lfs/wrapper.py
+++ b/hgext/lfs/wrapper.py
@@ -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
 
diff --git a/hgext/censor.py b/hgext/censor.py
--- a/hgext/censor.py
+++ b/hgext/censor.py
@@ -106,7 +106,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()