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.
2 Answers
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.
-
1All I want to do is launch a script that will update files on my server (via FTP) based on the working tree after I've pushed to that repo. So, it's three places: a local repo, another local repo, and an FTP location (non-git). When I push to one repo, I when would be the best time to launch such a script (I'll be using
git diff
to figure out what files to FTP over)?– trusktrMar 11, 2012 at 7:35 -
4post-receive is a super-set of the functionality of post-update, and it's after the heads are pushed so I imagine post-receive should be sufficient. Mar 12, 2012 at 2:35
-
1
-
@trusktr Thanks for the edit, I didn't notice that the tags were getting eaten -- just re-edited to escape the tag. Mar 12, 2012 at 6:05
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.
-
Hmm. Just to clarify. 'Lift this "hooks must consume their input fully" mandate.' -- does it mean exactly what it reads: it has not been lifted, but is just a wish to Santa, strangely misplaced in a commit message? Or is it actually "lifted", so we can finally celebrate? :) Thanks!– Sz.Jan 22, 2016 at 20:12
-
@Sz. It is lifted. That is because "A mandate that is not enforced strictly is not helping us to catch mistakes in hooks" As a consequence of that lifted mandate: "If a hook has a good reason to decide the outcome of its operation without reading the information we feed it, let it do so as it pleases."– VonCJan 22, 2016 at 20:16
-
OK, thanks. The problem was with the wording, the incorrect tense used in the message, which made it ambiguous. And git has taught me never to trust its ambiguous choices of words at face value. And now I can +1 your answer, which I can consider complete with your comment.– Sz.Jan 22, 2016 at 20:32