Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/transaction.py @ 48699:21ac6aedd5e5
transaction: do not rely on a global variable to post_finalize file
We can just add a new argument to the `addfilegenerator` function. This is more
explicit and therefor clearer and less error prone.
Differential Revision: https://phab.mercurial-scm.org/D12125
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 31 Jan 2022 18:38:15 +0100 |
parents | 568f63b5a30f |
children | 6000f5b25c9b |
comparison
equal
deleted
inserted
replaced
48698:568f63b5a30f | 48699:21ac6aedd5e5 |
---|---|
22 util, | 22 util, |
23 ) | 23 ) |
24 from .utils import stringutil | 24 from .utils import stringutil |
25 | 25 |
26 version = 2 | 26 version = 2 |
27 | |
28 # These are the file generators that should only be executed after the | |
29 # finalizers are done, since they rely on the output of the finalizers (like | |
30 # the changelog having been written). | |
31 postfinalizegenerators = { | |
32 b'bookmarks', | |
33 b'dirstate-0-key-pre', | |
34 b'dirstate-1-main', | |
35 b'dirstate-2-key-post', | |
36 } | |
37 | 27 |
38 GEN_GROUP_ALL = b'all' | 28 GEN_GROUP_ALL = b'all' |
39 GEN_GROUP_PRE_FINALIZE = b'prefinalize' | 29 GEN_GROUP_PRE_FINALIZE = b'prefinalize' |
40 GEN_GROUP_POST_FINALIZE = b'postfinalize' | 30 GEN_GROUP_POST_FINALIZE = b'postfinalize' |
41 | 31 |
337 """ | 327 """ |
338 self._addbackupentry((location, b'', tmpfile, False)) | 328 self._addbackupentry((location, b'', tmpfile, False)) |
339 | 329 |
340 @active | 330 @active |
341 def addfilegenerator( | 331 def addfilegenerator( |
342 self, genid, filenames, genfunc, order=0, location=b'' | 332 self, |
333 genid, | |
334 filenames, | |
335 genfunc, | |
336 order=0, | |
337 location=b'', | |
338 post_finalize=False, | |
343 ): | 339 ): |
344 """add a function to generates some files at transaction commit | 340 """add a function to generates some files at transaction commit |
345 | 341 |
346 The `genfunc` argument is a function capable of generating proper | 342 The `genfunc` argument is a function capable of generating proper |
347 content of each entry in the `filename` tuple. | 343 content of each entry in the `filename` tuple. |
360 generator will be executed. | 356 generator will be executed. |
361 | 357 |
362 The `location` arguments may be used to indicate the files are located | 358 The `location` arguments may be used to indicate the files are located |
363 outside of the the standard directory for transaction. It should match | 359 outside of the the standard directory for transaction. It should match |
364 one of the key of the `transaction.vfsmap` dictionary. | 360 one of the key of the `transaction.vfsmap` dictionary. |
361 | |
362 The `post_finalize` argument can be set to `True` for file generation | |
363 that must be run after the transaction has been finalized. | |
365 """ | 364 """ |
366 # For now, we are unable to do proper backup and restore of custom vfs | 365 # For now, we are unable to do proper backup and restore of custom vfs |
367 # but for bookmarks that are handled outside this mechanism. | 366 # but for bookmarks that are handled outside this mechanism. |
368 self._filegenerators[genid] = (order, filenames, genfunc, location) | 367 entry = (order, filenames, genfunc, location, post_finalize) |
368 self._filegenerators[genid] = entry | |
369 | 369 |
370 @active | 370 @active |
371 def removefilegenerator(self, genid): | 371 def removefilegenerator(self, genid): |
372 """reverse of addfilegenerator, remove a file generator function""" | 372 """reverse of addfilegenerator, remove a file generator function""" |
373 if genid in self._filegenerators: | 373 if genid in self._filegenerators: |
383 skip_pre = group == GEN_GROUP_POST_FINALIZE | 383 skip_pre = group == GEN_GROUP_POST_FINALIZE |
384 skip_post = group == GEN_GROUP_PRE_FINALIZE | 384 skip_post = group == GEN_GROUP_PRE_FINALIZE |
385 | 385 |
386 for id, entry in sorted(pycompat.iteritems(self._filegenerators)): | 386 for id, entry in sorted(pycompat.iteritems(self._filegenerators)): |
387 any = True | 387 any = True |
388 order, filenames, genfunc, location = entry | 388 order, filenames, genfunc, location, post_finalize = entry |
389 | 389 |
390 # for generation at closing, check if it's before or after finalize | 390 # for generation at closing, check if it's before or after finalize |
391 is_post = id in postfinalizegenerators | 391 if skip_post and post_finalize: |
392 if skip_post and is_post: | |
393 continue | 392 continue |
394 elif skip_pre and not is_post: | 393 elif skip_pre and not post_finalize: |
395 continue | 394 continue |
396 | 395 |
397 vfs = self._vfsmap[location] | 396 vfs = self._vfsmap[location] |
398 files = [] | 397 files = [] |
399 try: | 398 try: |