git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: What about allowing multiple hooks?

From
Anders Waldenborg <anders@0x63.nu>
Date
Jan 21, 2009, 20:35 UTC
Message-ID
<4977872E.70901@0x63.nu>
In-Reply-To
<7vd4f3z8xu.fsf@gitster.siamese.dyndns.org>
Junio C Hamano wrote:
Show 12 quoted lines
> I deliberately omitted support for multiple scripts in core git Porcelains
> to avoid this exact issue.  It is a huge can of worms and it is dubious if
> you can have a coherent and generic enough semantics.
> 
> In the meantime, you can have a single .git/hooks/pre-commit script that
> defines your own convention.  Maybe it uses .git/hooks/pre-commit.d/
> directory, full of scripts, and implements the semantics you want,
> including:
> 
>  (1) the execution order and the naming convention of the scripts (e.g.
>      they all live in pre-commit.d/ directory, and executed in ASCII byte
>      value order of their names);
 >
 >  (2) how their exit status combine together.

I need multiple hooks, so I've done some thinking about this, so I thought it may be a good idea to share this here.

I currently use configvalues to specify which hooks to run. For example this is how my post-receive looks:

data=$(cat)
git config --get-all hooks.post-receive.hook | while read hook; do
         $hook <<__EOF__
"$data"
__EOF__
done

Now none of my hooks wants to prevent update, so I don't care about return status. But it could easily be extended, for example by having some indicator per hook that can have the values (are these enough?):

  ignore - pretent that no failure was returned no matter what
  sufficient - if this hook suceeds end result is always sucess
  required - if this hook fails we fail, no more hooks are run
That could be done with the simple configvalue thing as follows:
git config -add hooks.post-receive.hook \
    "sufficient allow-repo-owner-to-do-anything.sh"
git config -add hooks.post-receive.hook \
    "required finegrained-access-control.sh"
git config -add hooks.post-receive.hook \
    "required allow-repo-owner-to-do-anything.sh"
git config -add hooks.post-receive.hook \
    "ignore send-mail.sh"
git config -add hooks.post-receive.hook \
    "ignore send-irc-notification.py"

One problem is that to change order one has to resort to manually editing config. So maybe something richer could be used:

[hooks "allow-repo-owner-to-do-anything"]
  cmd = /usr/share/git-hooks/allow-repo-owner-to-do-anything.sh
  enabled = 1
  type = post-receive
  mode = sufficient
  priority = 10
[hooks "mail"]
  cmd = /usr/share/git-hooks/allow-repo-owner-to-do-anything.sh
  enabled = 1
  type = post-receive
  mode = ignore
  priority = 1000
(this would even allow running hooks at same priority simultaneously)

Also then the hook's own config variables fits nicely in same section. (note that then each [hooks "x"] will be an instance that could use the same script, but different configvars)

  anders
Previous: Junio C HamanoNext: Johannes Schindelin
Message 6 of 10 in “What about allowing multiple hooks?”
  1. Marc WeberNov 21, 2008
  2. martin f krafftNov 21, 2008
  3. Rogan DawesNov 21, 2008
  4. Alexander PotashevJan 3, 2009
  5. Junio C HamanoJan 4, 2009
  6. Anders WaldenborgJan 21, 2009
  7. Johannes SchindelinJan 21, 2009
  8. Anders WaldenborgJan 21, 2009
  9. Johannes SchindelinJan 21, 2009
  10. Anders WaldenborgJan 22, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.