[PATCH 1 of 5] manifest: extract method for parsing manifest

Martin von Zweigbergk martinvonz at google.com
Tue Mar 31 00:19:38 UTC 2015


# HG changeset patch
# User Martin von Zweigbergk <martinvonz at google.com>
# Date 1427493763 25200
#      Fri Mar 27 15:02:43 2015 -0700
# Node ID 90ddd267bb1d2988195ba8240d8c7c9cf7bbf3fd
# Parent  d9cc0d84339732f87b072688b0624d49fdf5436a
manifest: extract method for parsing manifest

By extracting a method that generates (path, node, flags) tuples, we
can reuse the code for parsing a manifest without doing it via a
_lazymanifest like treemanifest currently does. It also prepares for
parsing the new manifest format.

Note that this makes parsing into treemanifest slower, since the
parsing is now always done in pure Python. Since treemanifests will be
expected (or even forced) to be used only with the new manifest
format, parsing via _lazymanifest was not an option anyway.

diff -r d9cc0d843397 -r 90ddd267bb1d mercurial/manifest.py
--- a/mercurial/manifest.py	Fri Mar 27 20:55:54 2015 -0700
+++ b/mercurial/manifest.py	Fri Mar 27 15:02:43 2015 -0700
@@ -11,6 +11,26 @@
 
 propertycache = util.propertycache
 
+def _parse(data):
+    """Generates (path, node, flags) tuples from a manifest text"""
+    # This method does a little bit of excessive-looking
+    # precondition checking. This is so that the behavior of this
+    # class exactly matches its C counterpart to try and help
+    # prevent surprise breakage for anyone that develops against
+    # the pure version.
+    if data and data[-1] != '\n':
+        raise ValueError('Manifest did not end in a newline.')
+    prev = None
+    for l in data.splitlines():
+        if prev is not None and prev > l:
+            raise ValueError('Manifest lines not in sorted order.')
+        prev = l
+        f, n = l.split('\0')
+        if len(n) > 40:
+            yield f, revlog.bin(n[:40]), n[40:]
+        else:
+            yield f, revlog.bin(n), ''
+
 class _lazymanifest(dict):
     """This is the pure implementation of lazymanifest.
 
@@ -18,24 +38,9 @@
     """
 
     def __init__(self, data):
-        # This init method does a little bit of excessive-looking
-        # precondition checking. This is so that the behavior of this
-        # class exactly matches its C counterpart to try and help
-        # prevent surprise breakage for anyone that develops against
-        # the pure version.
-        if data and data[-1] != '\n':
-            raise ValueError('Manifest did not end in a newline.')
         dict.__init__(self)
-        prev = None
-        for l in data.splitlines():
-            if prev is not None and prev > l:
-                raise ValueError('Manifest lines not in sorted order.')
-            prev = l
-            f, n = l.split('\0')
-            if len(n) > 40:
-                self[f] = revlog.bin(n[:40]), n[40:]
-            else:
-                self[f] = revlog.bin(n), ''
+        for f, n, fl in _parse(data):
+            self[f] = n, fl
 
     def __setitem__(self, k, v):
         node, flag = v
@@ -342,8 +347,7 @@
         # Using _lazymanifest here is a little slower than plain old dicts
         self._files = {}
         self._flags = {}
-        lm = _lazymanifest(text)
-        for f, n, fl in lm.iterentries():
+        for f, n, fl in _parse(text):
             self[f] = n
             if fl:
                 self.setflag(f, fl)


More information about the Mercurial-devel mailing list