Patchwork D3579: state: write the version number in plain text on top of state files

login
register
mail settings
Submitter phabricator
Date May 19, 2018, 2:52 p.m.
Message ID <90bd50962b1c3ff05428c0f8514aae1f@localhost.localdomain>
Download mbox | patch
Permalink /patch/31704/
State Not Applicable
Headers show

Comments

phabricator - May 19, 2018, 2:52 p.m.
pulkit updated this revision to Diff 8787.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3579?vs=8734&id=8787

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

AFFECTED FILES
  mercurial/state.py

CHANGE DETAILS




To: pulkit, #hg-reviewers, martinvonz
Cc: martinvonz, mercurial-devel
Yuya Nishihara - May 22, 2018, 11:12 a.m.
> -    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())

mercurial/state.py:65: undefined name 'iv'
mercurial/state.py:73: local variable 'version' is assigned to but never used

> +            except ValueError:
> +                raise error.ProgrammingError("unknown version of state file"
> +                                             " found")

Perhaps this should be a CorruptedState error. We don't know whether the
state file is good until reading it.
phabricator - May 22, 2018, 11:13 a.m.
yuja added a comment.


  > - 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())
  
  mercurial/state.py:65: undefined name 'iv'
  mercurial/state.py:73: local variable 'version' is assigned to but never used
  
  > +            except ValueError:
  >  +                raise error.ProgrammingError("unknown version of state file"
  >  +                                             " found")
  
  Perhaps this should be a CorruptedState error. We don't know whether the
  state file is good until reading it.

REPOSITORY
  rHG Mercurial

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

To: pulkit, #hg-reviewers, martinvonz
Cc: yuja, martinvonz, mercurial-devel
Yuya Nishihara - May 22, 2018, 12:13 p.m.
> mercurial/state.py:65: undefined name 'iv'
> mercurial/state.py:73: local variable 'version' is assigned to but never used

Queued the fixes.
phabricator - May 22, 2018, 12:13 p.m.
yuja added a comment.


  > mercurial/state.py:65: undefined name 'iv'
  >  mercurial/state.py:73: local variable 'version' is assigned to but never used
  
  Queued the fixes.

REPOSITORY
  rHG Mercurial

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

To: pulkit, #hg-reviewers, martinvonz
Cc: yuja, martinvonz, mercurial-devel

Patch

diff --git a/mercurial/state.py b/mercurial/state.py
--- a/mercurial/state.py
+++ b/mercurial/state.py
@@ -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):