Patchwork [2,of,8] dispatch: add post-command hook

login
register
mail settings
Submitter Durham Goode
Date May 6, 2013, 7:36 p.m.
Message ID <4da152c208d0f853bd03.1367869012@dev350.prn1.facebook.com>
Download mbox | patch
Permalink /patch/1559/
State Superseded, archived
Headers show

Comments

Durham Goode - May 6, 2013, 7:36 p.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1367429782 25200
#      Wed May 01 10:36:22 2013 -0700
# Node ID 4da152c208d0f853bd03781a5d7a9b9cc8716a19
# Parent  4ddd3b2b3407fe3344fd800ea11ab377a34b8ae6
dispatch: add post-command hook

This adds a 'post-command' hook after dispatch has finished with the command.
This gives extensions a place to clean up any state that lasts the entire
session.
Matt Mackall - May 6, 2013, 7:56 p.m.
On Mon, 2013-05-06 at 12:36 -0700, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1367429782 25200
> #      Wed May 01 10:36:22 2013 -0700
> # Node ID 4da152c208d0f853bd03781a5d7a9b9cc8716a19
> # Parent  4ddd3b2b3407fe3344fd800ea11ab377a34b8ae6
> dispatch: add post-command hook
> 
> This adds a 'post-command' hook after dispatch has finished with the command.
> This gives extensions a place to clean up any state that lasts the entire
> session.

How is this different than the existing post-* hooks?

How does this work with the command server, where a session lasts longer
than a command?
Durham Goode - May 6, 2013, 8:27 p.m.
On 5/6/13 12:56 PM, "Matt Mackall" <mpm@selenic.com> wrote:

>On Mon, 2013-05-06 at 12:36 -0700, Durham Goode wrote:
>> # HG changeset patch
>> # User Durham Goode <durham@fb.com>
>> # Date 1367429782 25200
>> #      Wed May 01 10:36:22 2013 -0700
>> # Node ID 4da152c208d0f853bd03781a5d7a9b9cc8716a19
>> # Parent  4ddd3b2b3407fe3344fd800ea11ab377a34b8ae6
>> dispatch: add post-command hook
>> 
>> This adds a 'post-command' hook after dispatch has finished with the
>>command.
>> This gives extensions a place to clean up any state that lasts the
>>entire
>> session.
>
>How is this different than the existing post-* hooks?
>
>How does this work with the command server, where a session lasts longer
>than a command?

The difference is that this hook fires for every command.  In my scenario
a network connection is opened when there is a cache miss and the
connection is kept open until this hook is fired.  There isn't one
particular command that I could subscribe to in this case.

For the command server, this hook will fire after every command.  That's
fine in my case, since I'd probably want the network connection to close
after each command anyway.

I could achieve this in the extension by wrapping dispatch.runcommand (so
this hook is not absolutely necessary), but it seemed like a globally
useful hook to have.
Matt Mackall - May 6, 2013, 9:30 p.m.
On Mon, 2013-05-06 at 20:27 +0000, Durham Goode wrote:
> On 5/6/13 12:56 PM, "Matt Mackall" <mpm@selenic.com> wrote:
> 
> >On Mon, 2013-05-06 at 12:36 -0700, Durham Goode wrote:
> >> # HG changeset patch
> >> # User Durham Goode <durham@fb.com>
> >> # Date 1367429782 25200
> >> #      Wed May 01 10:36:22 2013 -0700
> >> # Node ID 4da152c208d0f853bd03781a5d7a9b9cc8716a19
> >> # Parent  4ddd3b2b3407fe3344fd800ea11ab377a34b8ae6
> >> dispatch: add post-command hook
> >> 
> >> This adds a 'post-command' hook after dispatch has finished with the
> >>command.
> >> This gives extensions a place to clean up any state that lasts the
> >>entire
> >> session.
> >
> >How is this different than the existing post-* hooks?
> >
> >How does this work with the command server, where a session lasts longer
> >than a command?
> 
> The difference is that this hook fires for every command.  In my scenario
> a network connection is opened when there is a cache miss and the
> connection is kept open until this hook is fired.  There isn't one
> particular command that I could subscribe to in this case.
> 
> For the command server, this hook will fire after every command.  That's
> fine in my case, since I'd probably want the network connection to close
> after each command anyway.
> 
> I could achieve this in the extension by wrapping dispatch.runcommand (so
> this hook is not absolutely necessary), but it seemed like a globally
> useful hook to have.

I'm at about -0 on adding a hook for this. Perhaps the most legitimate
use of an after-every-command user-facing hook has already been
fulfilled by blackbox.

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -541,6 +541,8 @@ 
     # run post-hook, passing command result
     hook.hook(lui, repo, "post-%s" % cmd, False, args=" ".join(fullargs),
               result=ret, pats=cmdpats, opts=cmdoptions)
+    if repo:
+        hook.hook(lui, repo, 'post-command')
     return ret
 
 def _getlocal(ui, rpath):
diff --git a/tests/test-hook.t b/tests/test-hook.t
--- a/tests/test-hook.t
+++ b/tests/test-hook.t
@@ -489,6 +489,14 @@ 
   abort: raise abort from hook
   [255]
 
+post-command hook
+  $ echo '[hooks]' > .hg/hgrc
+  $ echo 'post-command = python:hooktests.passhook' >> .hg/hgrc
+  $ hg id
+  cb9a9f314b8b
+  hook args:
+    hooktype post-command
+
 make sure --traceback works
 
   $ echo '[hooks]' > .hg/hgrc