comparison mercurial/state.py @ 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 bdc4079ceb16
comparison
equal deleted inserted replaced
38105:18c6d8b565bf 38106:a0e4d654bceb
20 from __future__ import absolute_import 20 from __future__ import absolute_import
21 21
22 from .thirdparty import cbor 22 from .thirdparty import cbor
23 23
24 from . import ( 24 from . import (
25 error,
25 util, 26 util,
26 ) 27 )
27 28
28 class cmdstate(object): 29 class cmdstate(object):
29 """a wrapper class to store the state of commands like `rebase`, `graft`, 30 """a wrapper class to store the state of commands like `rebase`, `graft`,
49 50
50 def read(self): 51 def read(self):
51 """read the existing state file and return a dict of data stored""" 52 """read the existing state file and return a dict of data stored"""
52 return self._read() 53 return self._read()
53 54
54 def save(self, data): 55 def save(self, version, data):
55 """write all the state data stored to .hg/<filename> file 56 """write all the state data stored to .hg/<filename> file
56 57
57 we use third-party library cbor to serialize data to write in the file. 58 we use third-party library cbor to serialize data to write in the file.
58 """ 59 """
60 if not isinstance(version, int):
61 raise error.ProgrammingError("version of state file should be"
62 " an integer")
63
59 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: 64 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp:
65 fp.write('%d\n' % iv)
60 cbor.dump(self.opts, fp, canonical=True) 66 cbor.dump(self.opts, fp, canonical=True)
61 67
62 def _read(self): 68 def _read(self):
63 """reads the state file and returns a dictionary which contain 69 """reads the state file and returns a dictionary which contain
64 data in the same format as it was before storing""" 70 data in the same format as it was before storing"""
65 with self._repo.vfs(self.fname, 'rb') as fp: 71 with self._repo.vfs(self.fname, 'rb') as fp:
72 try:
73 version = int(fp.readline())
74 except ValueError:
75 raise error.ProgrammingError("unknown version of state file"
76 " found")
66 return cbor.load(fp) 77 return cbor.load(fp)
67 78
68 def delete(self): 79 def delete(self):
69 """drop the state file if exists""" 80 """drop the state file if exists"""
70 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) 81 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True)