问题
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.
回答1:
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
).
来源:https://stackoverflow.com/questions/30104343/multiple-git-hooks-for-the-same-trigger