changeset 38106:a0e4d654bceb

state: write the version number in plain text on top of state files We will soon be using CBOR format to write the data in state files. But we should not write the version number of the state files in CBOR format and we should rather write it in plain text because in future we can change the format of state files and we should be able to parse the version number of state file without requiring to understand a certain format. This will help us in making sure we have a good compatibility story with other versions of state files. Differential Revision: https://phab.mercurial-scm.org/D3579
author Pulkit Goyal <7895pulkit@gmail.com>
date Fri, 18 May 2018 16:28:45 +0530
parents 18c6d8b565bf
children 44ef9bb7ccd9
files mercurial/state.py
diffstat 1 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/state.py	Fri May 18 16:34:19 2018 +0530
+++ b/mercurial/state.py	Fri May 18 16:28:45 2018 +0530
@@ -22,6 +22,7 @@
 from .thirdparty import cbor
 
 from . import (
+    error,
     util,
 )
 
@@ -51,18 +52,28 @@
         """read the existing state file and return a dict of data stored"""
         return self._read()
 
-    def save(self, data):
+    def save(self, version, data):
         """write all the state data stored to .hg/<filename> file
 
         we use third-party library cbor to serialize data to write in the file.
         """
+        if not isinstance(version, int):
+            raise error.ProgrammingError("version of state file should be"
+                                         " an integer")
+
         with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp:
+            fp.write('%d\n' % iv)
             cbor.dump(self.opts, fp, canonical=True)
 
     def _read(self):
         """reads the state file and returns a dictionary which contain
         data in the same format as it was before storing"""
         with self._repo.vfs(self.fname, 'rb') as fp:
+            try:
+                version = int(fp.readline())
+            except ValueError:
+                raise error.ProgrammingError("unknown version of state file"
+                                             " found")
             return cbor.load(fp)
 
     def delete(self):