comparison mercurial/util.py @ 33446:fad6852cf879

histedit: extract InterventionRequired transaction handling to utils rebase will have similar logic, so let's extract it. Besides, it makes the histedit code more readable. We may want to parametrize acceptintervention() by the exception(s) that should result in transaction close. Differential Revision: https://phab.mercurial-scm.org/D66
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 12 Jul 2017 13:57:03 -0700
parents 0e114b992e02
children 9a2ee9591acc
comparison
equal deleted inserted replaced
33445:0491004e2233 33446:fad6852cf879
17 17
18 import bz2 18 import bz2
19 import calendar 19 import calendar
20 import codecs 20 import codecs
21 import collections 21 import collections
22 import contextlib
22 import datetime 23 import datetime
23 import errno 24 import errno
24 import gc 25 import gc
25 import hashlib 26 import hashlib
26 import imp 27 import imp
586 587
587 def __setitem__(self, key, value): 588 def __setitem__(self, key, value):
588 if key in self: 589 if key in self:
589 del self[key] 590 del self[key]
590 super(sortdict, self).__setitem__(key, value) 591 super(sortdict, self).__setitem__(key, value)
592
593 @contextlib.contextmanager
594 def acceptintervention(tr=None):
595 """A context manager that closes the transaction on InterventionRequired
596
597 If no transaction was provided, this simply runs the body and returns
598 """
599 if not tr:
600 yield
601 return
602 try:
603 yield
604 tr.close()
605 except error.InterventionRequired:
606 tr.close()
607 raise
608 finally:
609 tr.release()
591 610
592 class _lrucachenode(object): 611 class _lrucachenode(object):
593 """A node in a doubly linked list. 612 """A node in a doubly linked list.
594 613
595 Holds a reference to nodes on either side as well as a key-value 614 Holds a reference to nodes on either side as well as a key-value