Patchwork [04,of,10] transaction: add `writepending` logic

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 28, 2014, 3:41 p.m.
Message ID <7bc27f8375dcb9682a78.1414510878@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/6481/
State Accepted
Headers show

Comments

Pierre-Yves David - Oct. 28, 2014, 3:41 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1413605994 25200
#      Fri Oct 17 21:19:54 2014 -0700
# Branch stable
# Node ID 7bc27f8375dcb9682a78d2cccf46d7644dcef497
# Parent  5e6d7d3e80d823da9071c125fd3caf01cfc263be
transaction: add `writepending` logic

The content of the transaction must be flushed to disk before running hook. But
it must be flushed to special file so that the normal reader do not use them.
This logic is currently in the changelog only. We add some facility to register
such operation in the transaction itself.

Patch

diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -99,10 +99,14 @@  class transaction(object):
             opener.chmod(self.journal, createmode & 0666)
             opener.chmod(self.backupjournal, createmode & 0666)
 
         # hold file generations to be performed on commit
         self._filegenerators = {}
+        # hold callbalk to write pending data for hooks
+        self._pendingcallback = {}
+        # True is any pending data have been written ever
+        self._anypending = False
 
     def __del__(self):
         if self.journal:
             self._abort()
 
@@ -261,10 +265,30 @@  class transaction(object):
             self._abort()
 
     def running(self):
         return self.count > 0
 
+    def addpending(self, category, callback):
+        """add a callback to be called when the transaction is pending
+
+        Category is a unique identifier to allow overwriting old callback with
+        newer callback.
+        """
+        self._pendingcallback[category] = callback
+
+    @active
+    def writepending(self):
+        '''write pending file to temporary version
+
+        This is used to allow hooks to view transaction before commit'''
+        categories = sorted(self._pendingcallback)
+        for cat in categories:
+            # remove callback since the data will have been flushed
+            any = self._pendingcallback.pop(cat)()
+            self._anypending = self._anypending or any
+        return self._anypending
+
     @active
     def close(self):
         '''commit the transaction'''
         if self.count == 1 and self.onclose is not None:
             self._generatefiles()