Patchwork [2,of,2] hooks: add a 'txnabort' hook

login
register
mail settings
Submitter Pierre-Yves David
Date April 16, 2015, 9:44 a.m.
Message ID <56e29e610f1deddb2468.1429177443@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/8711/
State Accepted
Headers show

Comments

Pierre-Yves David - April 16, 2015, 9:44 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1429177009 14400
#      Thu Apr 16 05:36:49 2015 -0400
# Node ID 56e29e610f1deddb2468ca8aaf26fef0f739afe0
# Parent  bc4d8bc84a827ce2674f4f9cb650f5da60aca86e
hooks: add a 'txnabort' hook

This hook will be called whenever a transaction is aborted. This will make it
easy for people to clean up temporary content they may have created during a
transaction.
Matt Mackall - April 17, 2015, 12:45 a.m.
On Thu, 2015-04-16 at 05:44 -0400, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@fb.com>
> # Date 1429177009 14400
> #      Thu Apr 16 05:36:49 2015 -0400
> # Node ID 56e29e610f1deddb2468ca8aaf26fef0f739afe0
> # Parent  bc4d8bc84a827ce2674f4f9cb650f5da60aca86e
> hooks: add a 'txnabort' hook

These are queued for default, thanks.

Patch

diff --git a/mercurial/help/config.txt b/mercurial/help/config.txt
--- a/mercurial/help/config.txt
+++ b/mercurial/help/config.txt
@@ -831,10 +831,14 @@  variables it is passed are listed with n
   Run after any repository transaction has been commited. At this
   point, the transaction can no longer be rolled back. The hook will run
   after the lock is released. see ``pretxnclose`` docs for details about
   available variables.
 
+``txnabort``
+  Run when a transaction is aborted. see ``pretxnclose`` docs for details about
+  available variables.
+
 ``pretxnchangegroup``
   Run after a changegroup has been added via push, pull or unbundle,
   but before the transaction has been committed. Changegroup is
   visible to hook program. This lets you validate incoming changes
   before accepting them. Passed the ID of the first new changeset in
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -973,10 +973,16 @@  class localrepository(object):
             def hook():
                 reporef().hook('txnclose', throw=False, txnname=desc,
                                **tr2.hookargs)
             reporef()._afterlock(hook)
         tr.addfinalize('txnclose-hook', txnclosehook)
+        def txnaborthook(tr2):
+            """To be run if transaction is aborted
+            """
+            reporef().hook('txnabort', throw=False, txnname=desc,
+                           **tr2.hookargs)
+        tr.addabort('txnabort-hook', txnaborthook)
         self._transref = weakref.ref(tr)
         return tr
 
     def _journalfiles(self):
         return ((self.svfs, 'journal'),
diff --git a/tests/test-hook.t b/tests/test-hook.t
--- a/tests/test-hook.t
+++ b/tests/test-hook.t
@@ -13,10 +13,11 @@  commit hooks can see env vars
   > pre-cat = python "$TESTDIR/printenv.py" pre-cat
   > post-cat = python "$TESTDIR/printenv.py" post-cat
   > pretxnopen = sh -c "HG_LOCAL= HG_TAG= python \"$TESTDIR/printenv.py\" pretxnopen"
   > pretxnclose = sh -c "HG_LOCAL= HG_TAG= python \"$TESTDIR/printenv.py\" pretxnclose"
   > txnclose = sh -c "HG_LOCAL= HG_TAG= python \"$TESTDIR/printenv.py\" txnclose"
+  > txnabort = sh -c "HG_LOCAL= HG_TAG= python \"$TESTDIR/printenv.py\" txnabort"
   > EOF
   $ echo a > a
   $ hg add a
   $ hg commit -m a
   precommit hook: HG_PARENT1=0000000000000000000000000000000000000000
@@ -159,10 +160,11 @@  more there after
   pretxncommit hook: HG_NODE=6f611f8018c10e827fee6bd2bc807f937e761567 HG_PARENT1=539e4b31b6dc99b3cfbaa6b53cbc1c1f9a1e3a10 HG_PENDING=$TESTTMP/a
   5:6f611f8018c1
   5:6f611f8018c1
   pretxncommit.forbid hook: HG_NODE=6f611f8018c10e827fee6bd2bc807f937e761567 HG_PARENT1=539e4b31b6dc99b3cfbaa6b53cbc1c1f9a1e3a10 HG_PENDING=$TESTTMP/a
   transaction abort!
+  txnabort hook: HG_TXNID=TXN:* HG_TXNNAME=commit (glob)
   rollback completed
   abort: pretxncommit.forbid1 hook exited with status 1
   [255]
   $ hg -q tip
   4:539e4b31b6dc