I want to update a bare repo, and have it do something after something has been pushed to it using a hook. Which one should I use? The git-scm book says that they both fire after all refs have been updated, so I don't know what the difference is.
Pre-receive and post-receive: These are executed on the server receiving a push to do things like check for project conformance and to deploy after a push. Update: This is like a pre-receive, but operates on a branch-by-branch basis to execute code prior to each branch being accepted.
The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.
No. Hooks are per-repository and are never pushed.
update. This hook is invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. Just before updating the ref on the remote repository, the update hook is invoked. Its exit status determines the success or failure of the ref update.
From the documentation:
post-receive:
This supersedes the
<post-update>
hook in that it gets both old and new values of all the refs in addition to their names.
post-update:
The 'post-update' hook can tell what are the heads that were pushed, but it does not know what their original and updated values are, so it is a poor place to do log old..new. The
<post-receive>
hook does get both original and updated values of the refs. You might consider it instead if you need them.
A recent commit for Git 2.2+ (November 2014), by Junio C Hamano (gitster
) do mention:
The
pre-receive
andpost-receive
hooks were designed to be an improvement over old styleupdate
andpost-update
hooks, which take the update information on their command line and are limited by the command line length limit.The same information is fed from the standard input to pre/post-receive hooks instead to lift this limitation.
It has been mandatory for these new style hooks to consume the update information fully from the standard input stream. Otherwise, they would risk killing the receive-pack process via
SIGPIPE
.
It now adds:
If a hook does not want to look at all the information, it is easy to send its standard input to
/dev/null
(perhaps a niche use of hook might need to know only the fact that a push was made, without having to know what objects have been pushed to update which refs), and this has already been done by existing hooks that are written carefully.However, because there is no good way to consistently fail hooks that do not consume the input fully (a small push may result in a short update record that may fit within the pipe buffer, to which the
receive-pack
process may manage to write before the hook has a chance to exit without reading anything, which will not result in a death-by-SIGPIPE ofreceive-pack
), it can lead to a hard to diagnose "once in a blue moon" phantom failure.Lift this "hooks must consume their input fully" mandate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With