diff mercurial/transaction.py @ 36827:aff5996f3043

transaction: add a name and a __repr__ implementation (API) This has been useful for me for debugging. Differential Revision: https://phab.mercurial-scm.org/D2758
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 09 Mar 2018 14:30:15 -0800
parents ef345f9e4295
children 3e8952c0cb45
line wrap: on
line diff
--- a/mercurial/transaction.py	Fri Mar 09 16:10:55 2018 +0100
+++ b/mercurial/transaction.py	Fri Mar 09 14:30:15 2018 -0800
@@ -105,7 +105,7 @@
 class transaction(util.transactional):
     def __init__(self, report, opener, vfsmap, journalname, undoname=None,
                  after=None, createmode=None, validator=None, releasefn=None,
-                 checkambigfiles=None):
+                 checkambigfiles=None, name=r'<unnamed>'):
         """Begin a new transaction
 
         Begins a new transaction that allows rolling back writes in the event of
@@ -149,6 +149,8 @@
         if checkambigfiles:
             self.checkambigfiles.update(checkambigfiles)
 
+        self.names = [name]
+
         # A dict dedicated to precisely tracking the changes introduced in the
         # transaction.
         self.changes = {}
@@ -186,6 +188,11 @@
         # holds callbacks to call during abort
         self._abortcallback = {}
 
+    def __repr__(self):
+        name = r'/'.join(self.names)
+        return (r'<transaction name=%s, count=%d, usages=%d>' %
+                (name, self.count, self.usages))
+
     def __del__(self):
         if self.journal:
             self._abort()
@@ -365,14 +372,17 @@
         self.file.flush()
 
     @active
-    def nest(self):
+    def nest(self, name=r'<unnamed>'):
         self.count += 1
         self.usages += 1
+        self.names.append(name)
         return self
 
     def release(self):
         if self.count > 0:
             self.usages -= 1
+        if self.names:
+            self.names.pop()
         # if the transaction scopes are left without being closed, fail
         if self.count > 0 and self.usages == 0:
             self._abort()