diff --git a/mercurial/state.py b/mercurial/state.py --- a/mercurial/state.py +++ b/mercurial/state.py @@ -46,42 +46,35 @@ """ self._repo = repo self.fname = fname - if not opts: - self.opts = {} - else: - self.opts = opts - - def __nonzero__(self): - return self.exists() - - def __getitem__(self, key): - return self.opts[key] - def __setitem__(self, key, value): - updates = {key: value} - self.opts.update(updates) + def read(self): + """read the existing state file and return a dict of data stored""" + return self._read() - def load(self): - """load the existing state file into the class object""" - op = self._read() - self.opts.update(op) - - def addopts(self, opts): - """add more key-value pairs to the data stored by the object""" - self.opts.update(opts) - - def save(self): + def save(self, version, data): """write all the state data stored to .hg/ file we use third-party library cbor to serialize data to write in the file. """ + try: + iv = int(version) + except ValueError: + 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) 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):