3 # Copyright (c) 2007 Andy Parkins
5 # An example hook script to mail out commit update information. This hook
6 # sends emails listing new revisions to the repository introduced by the
7 # change being reported. The rule is that (for branch updates) each commit
8 # will appear on one email and one email only.
10 # This hook is stored in the contrib/hooks directory. Your distribution
11 # will have put this somewhere standard. You should make this script
12 # executable then link to it in the repository you would like to use it in.
13 # For example, on debian the hook is stored in
14 # /usr/share/git-core/contrib/hooks/post-receive-email:
16 # chmod a+x post-receive-email
17 # cd /path/to/your/repository.git
18 # ln -sf /usr/share/git-core/contrib/hooks/post-receive-email hooks/post-receive
20 # This hook script assumes it is enabled on the central repository of a
21 # project, with all users pushing only to it and not between each other. It
22 # will still work if you don't operate in that style, but it would become
23 # possible for the email to be from someone other than the person doing the
26 # To help with debugging and use on pre-v1.5.1 git servers, this script will
27 # also obey the interface of hooks/update, taking its arguments on the
28 # command line. Unfortunately, hooks/update is called once for each ref.
29 # To avoid firing one email per ref, this script just prints its output to
30 # the screen when used in this mode. The output can then be redirected if
36 # This is the list that all pushes will go to; leave it blank to not send
37 # emails for every ref update.
39 # This is the list that all pushes of annotated tags will go to. Leave it
40 # blank to default to the mailinglist field. The announce emails lists
41 # the short log summary of the changes since the last annotated tag.
42 # hooks.envelopesender
43 # If set then the -f option is passed to sendmail to allow the envelope
44 # sender address to be set
46 # All emails have their subjects prefixed with this prefix, or "[SCM]"
47 # if emailprefix is unset, to aid filtering
49 # The shell command used to format each revision in the email, with
50 # "%s" replaced with the commit id. Defaults to "git rev-list -1
51 # --pretty %s", displaying the commit id, author, date and log
52 # message. To list full patches separated by a blank line, you
53 # could set this to "git show -C %s; echo".
54 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
55 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
56 # Be careful if "..." contains things that will be expanded by shell "eval"
59 # The maximum number of lines that should be included in the generated
60 # email body. If not specified, there is no limit.
61 # Lines beyond the limit are suppressed and counted, and a final
62 # line is added indicating the number of suppressed lines.
64 # Alternate options for the git diff-tree invocation that shows changes.
65 # Default is "--stat --summary --find-copies-harder". Add -p to those
66 # options to include a unified diff of changes in addition to the usual
71 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
72 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
73 # give information for debugging.
76 # ---------------------------- Functions
79 # Function to prepare for email generation. This decides what type
80 # of update this is and whether an email should even be generated.
85 oldrev=$(git rev-parse $1)
86 newrev=$(git rev-parse $2)
94 if expr "$oldrev" : '0*$' >/dev/null
98 if expr "$newrev" : '0*$' >/dev/null
106 # --- Get the revision types
107 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
108 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
109 case "$change_type" in
112 rev_type="$newrev_type"
116 rev_type="$oldrev_type"
120 # The revision type tells us what type the commit is, combined with
121 # the location of the ref we can decide between
126 case "$refname","$rev_type" in
130 short_refname=${refname##refs/tags/}
134 refname_type="annotated tag"
135 short_refname=${refname##refs/tags/}
137 if [ -n "$announcerecipients" ]; then
138 recipients="$announcerecipients"
143 refname_type="branch"
144 short_refname=${refname##refs/heads/}
146 refs/remotes/*,commit)
148 refname_type="tracking branch"
149 short_refname=${refname##refs/remotes/}
150 echo >&2 "*** Push-update of tracking branch, $refname"
151 echo >&2 "*** - no email generated."
155 # Anything else (is there anything else?)
156 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
157 echo >&2 "*** - no email generated"
162 # Check if we've got anyone to send to
163 if [ -z "$recipients" ]; then
164 case "$refname_type" in
166 config_name="hooks.announcelist"
169 config_name="hooks.mailinglist"
172 echo >&2 "*** $config_name is not set so no email will be sent"
173 echo >&2 "*** for $refname update $oldrev->$newrev"
181 # Top level email generation function. This calls the appropriate
182 # body-generation routine after outputting the common header.
184 # Note this function doesn't actually generate any email output, that is
185 # taken care of by the functions it calls:
186 # - generate_email_header
187 # - generate_create_XXXX_email
188 # - generate_update_XXXX_email
189 # - generate_delete_XXXX_email
190 # - generate_email_footer
192 # Note also that this function cannot 'exit' from the script; when this
193 # function is running (in hook script mode), the send_mail() function
194 # is already executing in another process, connected via a pipe, and
195 # if this function exits without, whatever has been generated to that
196 # point will be sent as an email... even if nothing has been generated.
201 # The email subject will contain the best description of the ref
202 # that we can build from the parameters
203 describe=$(git describe $rev 2>/dev/null)
204 if [ -z "$describe" ]; then
208 generate_email_header
210 # Call the correct body generation function
212 case "$refname_type" in
213 "tracking branch"|branch)
221 if [ -z "$maxlines" ]; then
222 generate_${change_type}_${fn_name}_email
224 generate_${change_type}_${fn_name}_email | limit_lines $maxlines
227 generate_email_footer
230 generate_email_header()
232 # --- Email (all stdout will be the email)
236 Subject: ${emailprefix}$projectdesc $refname_type $short_refname ${change_type}d. $describe
237 X-Git-Refname: $refname
238 X-Git-Reftype: $refname_type
239 X-Git-Oldrev: $oldrev
240 X-Git-Newrev: $newrev
242 This is an automated email from the git hooks/post-receive script. It was
243 generated because a ref change was pushed to the repository containing
244 the project "$projectdesc".
246 The $refname_type, $short_refname has been ${change_type}d
250 generate_email_footer()
262 # --------------- Branches
265 # Called for the creation of a branch
267 generate_create_branch_email()
269 # This is a new branch and so oldrev is not valid
270 echo " at $newrev ($newrev_type)"
279 # Called for the change of a pre-existing branch
281 generate_update_branch_email()
284 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
286 # O is $oldrev for $refname
287 # N is $newrev for $refname
288 # X is a revision pointed to by some other ref, for which we may
289 # assume that an email has already been generated.
290 # In this case we want to issue an email containing only revisions
291 # 3, 4, and N. Given (almost) by
293 # git rev-list N ^O --not --all
295 # The reason for the "almost", is that the "--not --all" will take
296 # precedence over the "N", and effectively will translate to
298 # git rev-list N ^O ^X ^N
300 # So, we need to build up the list more carefully. git rev-parse
301 # will generate a list of revs that may be fed into git rev-list.
302 # We can get it to make the "--not --all" part and then filter out
305 # git rev-parse --not --all | grep -v N
307 # Then, using the --stdin switch to git rev-list we have effectively
310 # git rev-list N ^O ^X
312 # This leaves a problem when someone else updates the repository
313 # while this script is running. Their new value of the ref we're
314 # working on would be included in the "--not --all" output; and as
315 # our $newrev would be an ancestor of that commit, it would exclude
316 # all of our commits. What we really want is to exclude the current
317 # value of $refname from the --not list, rather than N itself. So:
319 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
321 # Get's us to something pretty safe (apart from the small time
322 # between refname being read, and git rev-parse running - for that,
326 # Next problem, consider this:
327 # * --- B --- * --- O ($oldrev)
329 # * --- X --- * --- N ($newrev)
331 # That is to say, there is no guarantee that oldrev is a strict
332 # subset of newrev (it would have required a --force, but that's
333 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
334 # Instead we find the common base of the two revs and list from
337 # As above, we need to take into account the presence of X; if
338 # another branch is already in the repository and points at some of
339 # the revisions that we are about to output - we don't want them.
340 # The solution is as before: git rev-parse output filtered.
342 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
344 # Tags pushed into the repository generate nice shortlog emails that
345 # summarise the commits between them and the previous tag. However,
346 # those emails don't include the full commit messages that we output
347 # for a branch update. Therefore we still want to output revisions
348 # that have been output on a tag email.
350 # Luckily, git rev-parse includes just the tool. Instead of using
351 # "--all" we use "--branches"; this has the added benefit that
352 # "remotes/" will be ignored as well.
354 # List all of the revisions that were removed by this update, in a
355 # fast-forward update, this list will be empty, because rev-list O
356 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
360 for rev in $(git rev-list $newrev..$oldrev)
362 revtype=$(git cat-file -t "$rev")
363 echo " discards $rev ($revtype)"
365 if [ -z "$rev" ]; then
369 # List all the revisions from baserev to newrev in a kind of
370 # "table-of-contents"; note this list can include revisions that
371 # have already had notification emails and is present to show the
372 # full detail of the change from rolling back the old revision to
373 # the base revision and then forward to the new revision
374 for rev in $(git rev-list $oldrev..$newrev)
376 revtype=$(git cat-file -t "$rev")
377 echo " via $rev ($revtype)"
380 if [ "$fast_forward" ]; then
381 echo " from $oldrev ($oldrev_type)"
383 # 1. Existing revisions were removed. In this case newrev
384 # is a subset of oldrev - this is the reverse of a
385 # fast-forward, a rewind
386 # 2. New revisions were added on top of an old revision,
387 # this is a rewind and addition.
389 # (1) certainly happened, (2) possibly. When (2) hasn't
390 # happened, we set a flag to indicate that no log printout
395 # Find the common ancestor of the old and new revisions and
396 # compare it with newrev
397 baserev=$(git merge-base $oldrev $newrev)
399 if [ "$baserev" = "$newrev" ]; then
400 echo "This update discarded existing revisions and left the branch pointing at"
401 echo "a previous point in the repository history."
403 echo " * -- * -- N ($newrev)"
405 echo " O -- O -- O ($oldrev)"
407 echo "The removed revisions are not necessarilly gone - if another reference"
408 echo "still refers to them they will stay in the repository."
411 echo "This update added new revisions after undoing existing revisions. That is"
412 echo "to say, the old revision is not a strict subset of the new revision. This"
413 echo "situation occurs when you --force push a change and generate a repository"
414 echo "containing something like this:"
416 echo " * -- * -- B -- O -- O -- O ($oldrev)"
418 echo " N -- N -- N ($newrev)"
420 echo "When this happens we assume that you've already had alert emails for all"
421 echo "of the O revisions, and so we here report only the revisions in the N"
422 echo "branch from the common base, B."
427 if [ -z "$rewind_only" ]; then
428 echo "Those revisions listed above that are new to this repository have"
429 echo "not appeared on any other notification email; so we list those"
430 echo "revisions in full, below."
436 # XXX: Need a way of detecting whether git rev-list actually
437 # outputted anything, so that we can issue a "no new
438 # revisions added by this update" message
442 echo "No new revisions were added by this update."
445 # The diffstat is shown from the old revision to the new revision.
446 # This is to show the truth of what happened in this change.
447 # There's no point showing the stat from the base to the new
448 # revision because the base is effectively a random revision at this
449 # point - the user will be interested in what this revision changed
450 # - including the undoing of previous revisions in the case of
451 # non-fast-forward updates.
453 echo "Summary of changes:"
454 git diff-tree $diffopts $oldrev..$newrev
458 # Called for the deletion of a branch
460 generate_delete_branch_email()
465 git show -s --pretty=oneline $oldrev
469 # --------------- Annotated tags
472 # Called for the creation of an annotated tag
474 generate_create_atag_email()
476 echo " at $newrev ($newrev_type)"
482 # Called for the update of an annotated tag (this is probably a rare event
483 # and may not even be allowed)
485 generate_update_atag_email()
487 echo " to $newrev ($newrev_type)"
488 echo " from $oldrev (which is now obsolete)"
494 # Called when an annotated tag is created or changed
496 generate_atag_email()
498 # Use git for-each-ref to pull out the individual fields from the
500 eval $(git for-each-ref --shell --format='
501 tagobject=%(*objectname)
502 tagtype=%(*objecttype)
504 tagged=%(taggerdate)' $refname
507 echo " tagging $tagobject ($tagtype)"
511 # If the tagged object is a commit, then we assume this is a
512 # release, and so we calculate which tag this tag is
514 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
516 if [ -n "$prevtag" ]; then
517 echo " replaces $prevtag"
521 echo " length $(git cat-file -s $tagobject) bytes"
524 echo " tagged by $tagger"
530 # Show the content of the tag message; this might contain a change
531 # log or release notes so is worth displaying.
532 git cat-file tag $newrev | sed -e '1,/^$/d'
537 # Only commit tags make sense to have rev-list operations
539 if [ -n "$prevtag" ]; then
540 # Show changes since the previous release
541 git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
543 # No previous tag, show all the changes since time
545 git rev-list --pretty=short $newrev | git shortlog
549 # XXX: Is there anything useful we can do for non-commit
558 # Called for the deletion of an annotated tag
560 generate_delete_atag_email()
565 git show -s --pretty=oneline $oldrev
569 # --------------- General references
572 # Called when any other type of reference is created (most likely a
575 generate_create_general_email()
577 echo " at $newrev ($newrev_type)"
579 generate_general_email
583 # Called when any other type of reference is updated (most likely a
586 generate_update_general_email()
588 echo " to $newrev ($newrev_type)"
591 generate_general_email
595 # Called for creation or update of any other type of reference
597 generate_general_email()
599 # Unannotated tags are more about marking a point than releasing a
600 # version; therefore we don't do the shortlog summary that we do for
601 # annotated tags above - we simply show that the point has been
602 # marked, and print the log message for the marked point for
605 # Note this section also catches any other reference type (although
606 # there aren't any) and deals with them in the same way.
609 if [ "$newrev_type" = "commit" ]; then
611 git show --no-color --root -s --pretty=medium $newrev
614 # What can we do here? The tag marks an object that is not
615 # a commit, so there is no log for us to display. It's
616 # probably not wise to output git cat-file as it could be a
617 # binary blob. We'll just say how big it is
618 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
623 # Called for the deletion of any other type of reference
625 generate_delete_general_email()
630 git show -s --pretty=oneline $oldrev
635 # --------------- Miscellaneous utilities
638 # Show new revisions as the user would like to see them in the email.
642 # This shows all log entries that are not already covered by
643 # another ref - i.e. commits that are now accessible from this
644 # ref that were previously not accessible
645 # (see generate_update_branch_email for the explanation of this
648 # Revision range passed to rev-list differs for new vs. updated
650 if [ "$change_type" = create ]
652 # Show all revisions exclusive to this (new) branch.
655 # Branch update; show revisions not part of $oldrev.
656 revspec=$oldrev..$newrev
659 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
661 git rev-parse --not $other_branches |
662 if [ -z "$custom_showrev" ]
664 git rev-list --pretty --stdin $revspec
666 git rev-list --stdin $revspec |
669 eval $(printf "$custom_showrev" $onerev)
679 while IFS="" read -r line; do
681 if [ $lines -gt $1 ]; then
682 skipped=$((skipped + 1))
684 printf "%s\n" "$line"
687 if [ $skipped -ne 0 ]; then
688 echo "... $skipped lines suppressed ..."
695 if [ -n "$envelopesender" ]; then
696 /usr/sbin/sendmail -t -f "$envelopesender"
698 /usr/sbin/sendmail -t
702 # ---------------------------- main()
705 LOGBEGIN="- Log -----------------------------------------------------------------"
706 LOGEND="-----------------------------------------------------------------------"
709 # Set GIT_DIR either from the working directory, or from the environment
711 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
712 if [ -z "$GIT_DIR" ]; then
713 echo >&2 "fatal: post-receive: GIT_DIR not set"
717 projectdesc=$(sed -ne '1p' "$GIT_DIR/description" 2>/dev/null)
718 # Check if the description is unchanged from it's default, and shorten it to
719 # a more manageable length if it is
720 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
722 projectdesc="UNNAMED PROJECT"
725 recipients=$(git config hooks.mailinglist)
726 announcerecipients=$(git config hooks.announcelist)
727 envelopesender=$(git config hooks.envelopesender)
728 emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')
729 custom_showrev=$(git config hooks.showrev)
730 maxlines=$(git config hooks.emailmaxlines)
731 diffopts=$(git config hooks.diffopts)
732 : ${diffopts:="--stat --summary --find-copies-harder"}
735 # Allow dual mode: run from the command line just like the update hook, or
736 # if no arguments are given then run as a hook script
737 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
738 # Output to the terminal in command line mode - if someone wanted to
739 # resend an email; they could redirect the output to sendmail
741 prep_for_email $2 $3 $1 && PAGER= generate_email
743 while read oldrev newrev refname
745 prep_for_email $oldrev $newrev $refname || continue
746 generate_email $maxlines | send_mail