diff mercurial/manifest.py @ 24572:b83679eb5f86

manifestv2: add support for reading new manifest format The new manifest format is designed to be smaller, in particular to produce smaller deltas. It stores hashes in binary and puts the hash on a new line (for smaller deltas). It also uses stem compression to save space for long paths. The format has room for metadata, but that's there only for future-proofing. The parser thus accepts any metadata and throws it away. For more information, see http://mercurial.selenic.com/wiki/ManifestV2Plan. The current manifest format doesn't allow an empty filename, so we use an empty filename on the first line to tell a manifest of the new format from the old. Since we still never write manifests in the new format, the added code is unused, but it is tested by test-manifest.py.
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 27 Mar 2015 22:26:41 -0700
parents 919f8ce040be
children 701d3554de0e
line wrap: on
line diff
--- a/mercurial/manifest.py	Tue Mar 31 22:45:45 2015 -0700
+++ b/mercurial/manifest.py	Fri Mar 27 22:26:41 2015 -0700
@@ -11,8 +11,7 @@
 
 propertycache = util.propertycache
 
-def _parse(data):
-    """Generates (path, node, flags) tuples from a manifest text"""
+def _parsev1(data):
     # 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
@@ -31,6 +30,34 @@
         else:
             yield f, revlog.bin(n), ''
 
+def _parsev2(data):
+    metadataend = data.find('\n')
+    # Just ignore metadata for now
+    pos = metadataend + 1
+    prevf = ''
+    while pos < len(data):
+        end = data.find('\n', pos + 1) # +1 to skip stem length byte
+        if end == -1:
+            raise ValueError('Manifest ended with incomplete file entry.')
+        stemlen = ord(data[pos])
+        items = data[pos + 1:end].split('\0')
+        f = prevf[:stemlen] + items[0]
+        if prevf > f:
+            raise ValueError('Manifest entries not in sorted order.')
+        fl = items[1]
+        # Just ignore metadata (items[2:] for now)
+        n = data[end + 1:end + 21]
+        yield f, n, fl
+        pos = end + 22
+        prevf = f
+
+def _parse(data):
+    """Generates (path, node, flags) tuples from a manifest text"""
+    if data.startswith('\0'):
+        return iter(_parsev2(data))
+    else:
+        return iter(_parsev1(data))
+
 def _text(it):
     """Given an iterator over (path, node, flags) tuples, returns a manifest
     text"""
@@ -116,7 +143,13 @@
 
 class manifestdict(object):
     def __init__(self, data=''):
-        self._lm = _lazymanifest(data)
+        if data.startswith('\0'):
+            #_lazymanifest can not parse v2
+            self._lm = _lazymanifest('')
+            for f, n, fl in _parsev2(data):
+                self._lm[f] = n, fl
+        else:
+            self._lm = _lazymanifest(data)
 
     def __getitem__(self, key):
         return self._lm[key][0]