Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple git hooks for the same trigger

Tags:

git

githooks

I have a post-checkout hook that I use locally in all of my repos (it renames my tmux session to repo-name/branch-name)

For a project I am working on, we just added a post-checkout hook that we're asking the whole team to use.

I don't want to add my personal hook's logic to the team-wide hook, because it's not useful to everyone, but I also don't want to give it up.

Is there a way to have more than one script execute on a single git-hook trigger? I want every git checkout to execute the teamwide post-checkout hook and execute my personal post-checkout hook. I can't have two files named the same thing -- is there a way to get around that?

Update: A good approach is, "make post-checkout call the two other scripts. I like this idea, and it may be the solution.

However, right now we have an automated setup step that copies post-checkout into the hooks directory. If possible, I'd like to do this in a way that doesn't interfere with the existing team setup, and doesn't require manual tweaking on my part if I run that install step again later.

If that's not possible, that's cool, but I'm curious about even more creative solutions.

like image 204
asfallows Avatar asked May 07 '15 14:05

asfallows


People also ask

Are git hooks synced?

Git hooks let developers automate expectations and workflows–terrific news for supporting ease-of-use and consistency across a development team. Unfortunately, git doesn't automatically synchronize hooks between project contributors.

Are git hooks version controlled?

git/hooks directory isn't cloned with the rest of your project, nor is it under version control. A simple solution to both of these problems is to store your hooks in the actual project directory (above the . git directory). This lets you edit them like any other version-controlled file.

What is Githooks?

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.

Can Git hook scripts be managed along with the repository?

So, you can write a Python or Go code to achieve your goals, and place it under the hooks folder. It will work, but it will not be managed along with the repository. The parameter will provide your script which hook was triggered, and you can differentiate it inside your code.


1 Answers

Sure. Create a wrapper post-checkout hook script that calls the other scripts:

#!/bin/sh

$GIT_DIR/hooks/my-tmux-post-checkout "$@"
$GIT_DIR/hooks/corporate-post-checkout "$@"

You could get fancier and iterate over an arbitrary number of scripts in a post-checkout.d directory or something, but the basic idea is the same.

Update for Steve

For scripts that expect input on stdin:

#!/bin/sh

tmpfile=$(mktemp hookXXXXXX)
trap "rm -f $tmpfile" EXIT
cat > $tmpfile

$GIT_DIR/hooks/my-tmux-post-checkout "$@" < $tmpfile
$GIT_DIR/hooks/corporate-post-checkout "$@" < $tmpfile

This should actually be harmless to use for the first case as well, although if you test it by running it manually you would need to make sure you always redirect stdin from somewhere (possibly /dev/null).

like image 73
larsks Avatar answered Oct 17 '22 08:10

larsks