Show More
@@ -1,74 +1,74 | |||
|
1 | 1 | # state.py - writing and reading state files in Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2018 Pulkit Goyal <pulkitmgoyal@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | """ |
|
9 | 9 | This file contains class to wrap the state for commands and other |
|
10 | 10 | related logic. |
|
11 | 11 | |
|
12 | 12 | All the data related to the command state is stored as dictionary in the object. |
|
13 | 13 | The class has methods using which the data can be stored to disk in a file under |
|
14 | 14 | .hg/ directory. |
|
15 | 15 | |
|
16 | 16 | We store the data on disk in cbor, for which we use the third party cbor library |
|
17 | 17 | to serialize and deserialize data. |
|
18 | 18 | """ |
|
19 | 19 | |
|
20 | 20 | from __future__ import absolute_import |
|
21 | 21 | |
|
22 | 22 | from .thirdparty import cbor |
|
23 | 23 | |
|
24 | 24 | from . import ( |
|
25 | 25 | util, |
|
26 | 26 | ) |
|
27 | 27 | |
|
28 | 28 | class cmdstate(object): |
|
29 | 29 | """a wrapper class to store the state of commands like `rebase`, `graft`, |
|
30 | 30 | `histedit`, `shelve` etc. Extensions can also use this to write state files. |
|
31 | 31 | |
|
32 | 32 | All the data for the state is stored in the form of key-value pairs in a |
|
33 | 33 | dictionary. |
|
34 | 34 | |
|
35 | 35 | The class object can write all the data to a file in .hg/ directory and |
|
36 | 36 | can populate the object data reading that file. |
|
37 | 37 | |
|
38 | 38 | Uses cbor to serialize and deserialize data while writing and reading from |
|
39 | 39 | disk. |
|
40 | 40 | """ |
|
41 | 41 | |
|
42 | 42 | def __init__(self, repo, fname, opts=None): |
|
43 | 43 | """ repo is the repo object |
|
44 | 44 | fname is the file name in which data should be stored in .hg directory |
|
45 | 45 | opts is a dictionary of data of the statefile |
|
46 | 46 | """ |
|
47 | 47 | self._repo = repo |
|
48 | 48 | self.fname = fname |
|
49 | 49 | |
|
50 | 50 | def read(self): |
|
51 | 51 | """read the existing state file and return a dict of data stored""" |
|
52 | 52 | return self._read() |
|
53 | 53 | |
|
54 | 54 | def save(self, data): |
|
55 | 55 | """write all the state data stored to .hg/<filename> file |
|
56 | 56 | |
|
57 | 57 | we use third-party library cbor to serialize data to write in the file. |
|
58 | 58 | """ |
|
59 | 59 | with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: |
|
60 | cbor.dump(self.opts, fp) | |
|
60 | cbor.dump(self.opts, fp, canonical=True) | |
|
61 | 61 | |
|
62 | 62 | def _read(self): |
|
63 | 63 | """reads the state file and returns a dictionary which contain |
|
64 | 64 | data in the same format as it was before storing""" |
|
65 | 65 | with self._repo.vfs(self.fname, 'rb') as fp: |
|
66 | 66 | return cbor.load(fp) |
|
67 | 67 | |
|
68 | 68 | def delete(self): |
|
69 | 69 | """drop the state file if exists""" |
|
70 | 70 | util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) |
|
71 | 71 | |
|
72 | 72 | def exists(self): |
|
73 | 73 | """check whether the state file exists or not""" |
|
74 | 74 | return self._repo.vfs.exists(self.fname) |
General Comments 0
You need to be logged in to leave comments.
Login now