diff -r 5904511fc9f8 -r bbbbd3c30bfc mercurial/util.py --- a/mercurial/util.py Mon Aug 14 16:26:36 2017 -0700 +++ b/mercurial/util.py Fri Jul 28 22:42:10 2017 -0700 @@ -15,6 +15,7 @@ from __future__ import absolute_import +import abc import bz2 import calendar import codecs @@ -592,6 +593,31 @@ for k, v in src: self[k] = v +class transactional(object): + """Base class for making a transactional type into a context manager.""" + __metaclass__ = abc.ABCMeta + + @abc.abstractmethod + def close(self): + """Successfully closes the transaction.""" + + @abc.abstractmethod + def release(self): + """Marks the end of the transaction. + + If the transaction has not been closed, it will be aborted. + """ + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + try: + if exc_type is None: + self.close() + finally: + self.release() + @contextlib.contextmanager def acceptintervention(tr=None): """A context manager that closes the transaction on InterventionRequired