comparison hgext/shelve.py @ 32284:16d424b97125

shelve: refactor shelvestate loading This is a preparatory patch which separates file reading from the minimal validation we have (like turning version into int and checking that this version is supported). The purpose of this patch is to be able to read statefile form simplekeyvaluefile, which is implemented in the following patch.
author Kostia Balytskyi <ikostia@fb.com>
date Sun, 14 May 2017 14:15:07 -0700
parents 1c398f7f4aa4
children fe3105e6e051
comparison
equal deleted inserted replaced
32283:8a1ff5ed620e 32284:16d424b97125
174 # colon is essential to differentiate from a real bookmark name 174 # colon is essential to differentiate from a real bookmark name
175 _noactivebook = ':no-active-bookmark' 175 _noactivebook = ':no-active-bookmark'
176 176
177 @classmethod 177 @classmethod
178 def load(cls, repo): 178 def load(cls, repo):
179 # Order is important, because old shelvestate file uses it
180 # to detemine values of fields (i.g. version is on the first line,
181 # name is on the second and so forth). Please do not change.
182 keys = ['version', 'name', 'originalwctx', 'pendingctx', 'parents',
183 'nodestoremove', 'branchtorestore', 'keep', 'activebook']
184 d = {}
179 fp = repo.vfs(cls._filename) 185 fp = repo.vfs(cls._filename)
180 try: 186 try:
181 version = int(fp.readline().strip()) 187 for key in keys:
182 188 d[key] = fp.readline().strip()
183 if version != cls._version:
184 raise error.Abort(_('this version of shelve is incompatible '
185 'with the version used in this repo'))
186 name = fp.readline().strip()
187 wctx = nodemod.bin(fp.readline().strip())
188 pendingctx = nodemod.bin(fp.readline().strip())
189 parents = [nodemod.bin(h) for h in fp.readline().split()]
190 nodestoremove = [nodemod.bin(h) for h in fp.readline().split()]
191 branchtorestore = fp.readline().strip()
192 keep = fp.readline().strip() == cls._keep
193 activebook = fp.readline().strip()
194 except (ValueError, TypeError) as err:
195 raise error.CorruptedState(str(err))
196 finally: 189 finally:
197 fp.close() 190 fp.close()
198 191
192 # some basic syntactic verification and transformation
193 try:
194 d['version'] = int(d['version'])
195 if d['version'] != cls._version:
196 raise error.Abort(_('this version of shelve is incompatible '
197 'with the version used in this repo'))
198 d['originalwctx'] = nodemod.bin(d['originalwctx'])
199 d['pendingctx'] = nodemod.bin(d['pendingctx'])
200 d['parents'] = [nodemod.bin(h)
201 for h in d['parents'].split(' ')]
202 d['nodestoremove'] = [nodemod.bin(h)
203 for h in d['nodestoremove'].split(' ')]
204 except (ValueError, TypeError, KeyError) as err:
205 raise error.CorruptedState(str(err))
206
199 try: 207 try:
200 obj = cls() 208 obj = cls()
201 obj.name = name 209 obj.name = d['name']
202 obj.wctx = repo[wctx] 210 obj.wctx = repo[d['originalwctx']]
203 obj.pendingctx = repo[pendingctx] 211 obj.pendingctx = repo[d['pendingctx']]
204 obj.parents = parents 212 obj.parents = d['parents']
205 obj.nodestoremove = nodestoremove 213 obj.nodestoremove = d['nodestoremove']
206 obj.branchtorestore = branchtorestore 214 obj.branchtorestore = d.get('branchtorestore', '')
207 obj.keep = keep 215 obj.keep = d.get('keep') == cls._keep
208 obj.activebookmark = '' 216 obj.activebookmark = ''
209 if activebook != cls._noactivebook: 217 if d.get('activebook', '') != cls._noactivebook:
210 obj.activebookmark = activebook 218 obj.activebookmark = d.get('activebook', '')
211 except error.RepoLookupError as err: 219 except (error.RepoLookupError, KeyError) as err:
212 raise error.CorruptedState(str(err)) 220 raise error.CorruptedState(str(err))
213 221
214 return obj 222 return obj
215 223
216 @classmethod 224 @classmethod