comparison mercurial/state.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 8c4f32b907e6
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
23 23
24 from . import ( 24 from . import (
25 error, 25 error,
26 util, 26 util,
27 ) 27 )
28 from .utils import ( 28 from .utils import cborutil
29 cborutil, 29
30 )
31 30
32 class cmdstate(object): 31 class cmdstate(object):
33 """a wrapper class to store the state of commands like `rebase`, `graft`, 32 """a wrapper class to store the state of commands like `rebase`, `graft`,
34 `histedit`, `shelve` etc. Extensions can also use this to write state files. 33 `histedit`, `shelve` etc. Extensions can also use this to write state files.
35 34
58 """write all the state data stored to .hg/<filename> file 57 """write all the state data stored to .hg/<filename> file
59 58
60 we use third-party library cbor to serialize data to write in the file. 59 we use third-party library cbor to serialize data to write in the file.
61 """ 60 """
62 if not isinstance(version, int): 61 if not isinstance(version, int):
63 raise error.ProgrammingError("version of state file should be" 62 raise error.ProgrammingError(
64 " an integer") 63 "version of state file should be" " an integer"
64 )
65 65
66 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: 66 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp:
67 fp.write('%d\n' % version) 67 fp.write('%d\n' % version)
68 for chunk in cborutil.streamencode(data): 68 for chunk in cborutil.streamencode(data):
69 fp.write(chunk) 69 fp.write(chunk)
73 data in the same format as it was before storing""" 73 data in the same format as it was before storing"""
74 with self._repo.vfs(self.fname, 'rb') as fp: 74 with self._repo.vfs(self.fname, 'rb') as fp:
75 try: 75 try:
76 int(fp.readline()) 76 int(fp.readline())
77 except ValueError: 77 except ValueError:
78 raise error.CorruptedState("unknown version of state file" 78 raise error.CorruptedState(
79 " found") 79 "unknown version of state file" " found"
80 )
80 81
81 return cborutil.decodeall(fp.read())[0] 82 return cborutil.decodeall(fp.read())[0]
82 83
83 def delete(self): 84 def delete(self):
84 """drop the state file if exists""" 85 """drop the state file if exists"""
85 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) 86 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True)
86 87
87 def exists(self): 88 def exists(self):
88 """check whether the state file exists or not""" 89 """check whether the state file exists or not"""
89 return self._repo.vfs.exists(self.fname) 90 return self._repo.vfs.exists(self.fname)
91
90 92
91 class _statecheck(object): 93 class _statecheck(object):
92 """a utility class that deals with multistep operations like graft, 94 """a utility class that deals with multistep operations like graft,
93 histedit, bisect, update etc and check whether such commands 95 histedit, bisect, update etc and check whether such commands
94 are in an unfinished conditition or not and return appropriate message 96 are in an unfinished conditition or not and return appropriate message
95 and hint. 97 and hint.
96 It also has the ability to register and determine the states of any new 98 It also has the ability to register and determine the states of any new
97 multistep operation or multistep command extension. 99 multistep operation or multistep command extension.
98 """ 100 """
99 101
100 def __init__(self, opname, fname, clearable, allowcommit, reportonly, 102 def __init__(
101 continueflag, stopflag, cmdmsg, cmdhint, statushint, 103 self,
102 abortfunc, continuefunc): 104 opname,
105 fname,
106 clearable,
107 allowcommit,
108 reportonly,
109 continueflag,
110 stopflag,
111 cmdmsg,
112 cmdhint,
113 statushint,
114 abortfunc,
115 continuefunc,
116 ):
103 self._opname = opname 117 self._opname = opname
104 self._fname = fname 118 self._fname = fname
105 self._clearable = clearable 119 self._clearable = clearable
106 self._allowcommit = allowcommit 120 self._allowcommit = allowcommit
107 self._reportonly = reportonly 121 self._reportonly = reportonly
116 def statusmsg(self): 130 def statusmsg(self):
117 """returns the hint message corresponding to the command for 131 """returns the hint message corresponding to the command for
118 hg status --verbose 132 hg status --verbose
119 """ 133 """
120 if not self._statushint: 134 if not self._statushint:
121 hint = (_('To continue: hg %s --continue\n' 135 hint = _(
122 'To abort: hg %s --abort') % (self._opname, 136 'To continue: hg %s --continue\n'
123 self._opname)) 137 'To abort: hg %s --abort'
138 ) % (self._opname, self._opname)
124 if self._stopflag: 139 if self._stopflag:
125 hint = hint + (_('\nTo stop: hg %s --stop') % 140 hint = hint + (
126 (self._opname)) 141 _('\nTo stop: hg %s --stop') % (self._opname)
142 )
127 return hint 143 return hint
128 return self._statushint 144 return self._statushint
129 145
130 def hint(self): 146 def hint(self):
131 """returns the hint message corresponding to an interrupted 147 """returns the hint message corresponding to an interrupted
132 operation 148 operation
133 """ 149 """
134 if not self._cmdhint: 150 if not self._cmdhint:
135 return (_("use 'hg %s --continue' or 'hg %s --abort'") % 151 return _("use 'hg %s --continue' or 'hg %s --abort'") % (
136 (self._opname, self._opname)) 152 self._opname,
153 self._opname,
154 )
137 return self._cmdhint 155 return self._cmdhint
138 156
139 def msg(self): 157 def msg(self):
140 """returns the status message corresponding to the command""" 158 """returns the status message corresponding to the command"""
141 if not self._cmdmsg: 159 if not self._cmdmsg:
153 if self._opname == 'merge': 171 if self._opname == 'merge':
154 return len(repo[None].parents()) > 1 172 return len(repo[None].parents()) > 1
155 else: 173 else:
156 return repo.vfs.exists(self._fname) 174 return repo.vfs.exists(self._fname)
157 175
176
158 # A list of statecheck objects for multistep operations like graft. 177 # A list of statecheck objects for multistep operations like graft.
159 _unfinishedstates = [] 178 _unfinishedstates = []
160 179
161 def addunfinished(opname, fname, clearable=False, allowcommit=False, 180
162 reportonly=False, continueflag=False, stopflag=False, 181 def addunfinished(
163 cmdmsg="", cmdhint="", statushint="", abortfunc=None, 182 opname,
164 continuefunc=None): 183 fname,
184 clearable=False,
185 allowcommit=False,
186 reportonly=False,
187 continueflag=False,
188 stopflag=False,
189 cmdmsg="",
190 cmdhint="",
191 statushint="",
192 abortfunc=None,
193 continuefunc=None,
194 ):
165 """this registers a new command or operation to unfinishedstates 195 """this registers a new command or operation to unfinishedstates
166 opname is the name the command or operation 196 opname is the name the command or operation
167 fname is the file name in which data should be stored in .hg directory. 197 fname is the file name in which data should be stored in .hg directory.
168 It is None for merge command. 198 It is None for merge command.
169 clearable boolean determines whether or not interrupted states can be 199 clearable boolean determines whether or not interrupted states can be
187 'To abort: hg cmdname --abort') is not desired 217 'To abort: hg cmdname --abort') is not desired
188 abortfunc stores the function required to abort an unfinished state. 218 abortfunc stores the function required to abort an unfinished state.
189 continuefunc stores the function required to finish an interrupted 219 continuefunc stores the function required to finish an interrupted
190 operation. 220 operation.
191 """ 221 """
192 statecheckobj = _statecheck(opname, fname, clearable, allowcommit, 222 statecheckobj = _statecheck(
193 reportonly, continueflag, stopflag, cmdmsg, 223 opname,
194 cmdhint, statushint, abortfunc, continuefunc) 224 fname,
225 clearable,
226 allowcommit,
227 reportonly,
228 continueflag,
229 stopflag,
230 cmdmsg,
231 cmdhint,
232 statushint,
233 abortfunc,
234 continuefunc,
235 )
195 if opname == 'merge': 236 if opname == 'merge':
196 _unfinishedstates.append(statecheckobj) 237 _unfinishedstates.append(statecheckobj)
197 else: 238 else:
198 _unfinishedstates.insert(0, statecheckobj) 239 _unfinishedstates.insert(0, statecheckobj)
199 240
241
200 addunfinished( 242 addunfinished(
201 'update', fname='updatestate', clearable=True, 243 'update',
244 fname='updatestate',
245 clearable=True,
202 cmdmsg=_('last update was interrupted'), 246 cmdmsg=_('last update was interrupted'),
203 cmdhint=_("use 'hg update' to get a consistent checkout"), 247 cmdhint=_("use 'hg update' to get a consistent checkout"),
204 statushint=_("To continue: hg update .") 248 statushint=_("To continue: hg update ."),
205 ) 249 )
206 addunfinished( 250 addunfinished(
207 'bisect', fname='bisect.state', allowcommit=True, reportonly=True, 251 'bisect',
208 statushint=_('To mark the changeset good: hg bisect --good\n' 252 fname='bisect.state',
209 'To mark the changeset bad: hg bisect --bad\n' 253 allowcommit=True,
210 'To abort: hg bisect --reset\n') 254 reportonly=True,
255 statushint=_(
256 'To mark the changeset good: hg bisect --good\n'
257 'To mark the changeset bad: hg bisect --bad\n'
258 'To abort: hg bisect --reset\n'
259 ),
211 ) 260 )
261
212 262
213 def getrepostate(repo): 263 def getrepostate(repo):
214 # experimental config: commands.status.skipstates 264 # experimental config: commands.status.skipstates
215 skip = set(repo.ui.configlist('commands', 'status.skipstates')) 265 skip = set(repo.ui.configlist('commands', 'status.skipstates'))
216 for state in _unfinishedstates: 266 for state in _unfinishedstates: