4 # Thorsten “mirabilos” Glaser <thorsten.glaser@teckids.org>
5 # Copyright © 2011, 2012, 2013, 2014
6 # Thorsten “mirabilos” Glaser <t.glaser@tarent.de>
8 # Copyright (c) 2007 Andy Parkins
10 # This code is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License version 2 as
12 # published by the Free Software Foundation.
14 # An example hook script to mail out commit update information.
16 # NOTE: This script is no longer under active development. There
17 # is another script, git-multimail, which is more capable and
18 # configurable and is largely backwards-compatible with this script;
19 # please see "contrib/hooks/multimail/". For instructions on how to
20 # migrate from post-receive-email to git-multimail, please see
21 # "README.migrate-from-post-receive-email" in that directory.
23 # This hook sends emails listing new revisions to the repository
24 # introduced by the change being reported. The rule is that (for
25 # branch updates) each commit will appear on one email and one email
28 # This hook is stored in the contrib/hooks directory. Your distribution
29 # will have put this somewhere standard. You should make this script
30 # executable then link to it in the repository you would like to use it in.
31 # For example, on Debian the hook is stored in
32 # /usr/share/git-core/contrib/hooks/post-receive-email
33 # but as /etc/gforge/plugins/scmgit/post-receive-email.sh in FusionForge.
35 # chmod a+x post-receive-email
36 # cd /path/to/your/repository.git
37 # ln -sf /usr/share/git-core/contrib/hooks/post-receive-email hooks/post-receive
39 # This hook script needs the “en_US.UTF-8” locale set up on the server.
40 # The FusionForge version works with a POSIX shell.
42 # This hook script assumes it is enabled on the central repository of a
43 # project, with all users pushing only to it and not between each other. It
44 # will still work if you don't operate in that style, but it would become
45 # possible for the email to be from someone other than the person doing the
48 # To help with debugging and use on pre-v1.5.1 git servers, this script will
49 # also obey the interface of hooks/update, taking its arguments on the
50 # command line. Unfortunately, hooks/update is called once for each ref.
51 # To avoid firing one email per ref, this script just prints its output to
52 # the screen when used in this mode. The output can then be redirected if
58 # This is the list that all pushes will go to; leave it blank to not send
59 # emails for every ref update.
61 # This is the list that all pushes of annotated tags will go to. Leave it
62 # blank to default to the mailinglist field. The announce emails lists
63 # the short log summary of the changes since the last annotated tag.
65 # If set, replies are requested to be directed to this eMail address.
66 # hooks.envelopesender
67 # If set then the -f option is passed to sendmail to allow the envelope
68 # sender address to be set. This is also used as Header From.
70 # If set, e.g. to '[SCM] ', all eMails have their subjects prefixed
71 # with this string to aid filtering.
73 # If set, this is used in the Subject instead of the description file.
75 # The shell command used to format each revision in the email, with
76 # "%s" replaced with the commit id. Defaults to "git rev-list -1
77 # --pretty %s", displaying the commit id, author, date and log
78 # message. To list full patches separated by a blank line, you
79 # could set this to "git show -C %s; echo". If you do that, make
80 # sure to remove the "-p" from hooks.diffopts to avoid getting
81 # two sets of diffs (the showrev diff is per commit, whereas the
82 # diffopts patch will show what really happened in the push as
84 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
85 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
86 # Be careful if "..." contains things that will be expanded by shell "eval"
89 # The maximum number of lines that should be included in the generated
90 # email body. If not specified, there is no limit.
91 # Lines beyond the limit are suppressed and counted, and a final
92 # line is added indicating the number of suppressed lines.
94 # Alternate options for the git diff-tree invocation that shows changes.
95 # Default is "-p --stat --summary --find-copies-harder". Remove -p from
96 # those options to skil including a unified diff of changes in addition
97 # to the summary output. See hooks.showrev for more discussion on this.
99 # Full path to a configuration file for kgb-client, or unset/empty.
100 # If set, kgb-client will be invoked.
104 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
105 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
106 # give information for debugging.
109 # ---------------------------- Environment Sanitisation
113 unset LANGUAGE LC_ALL
114 LANG=C LC_ADDRESS=C LC_COLLATE=C LC_CTYPE=en_US.UTF-8 \
115 LC_IDENTIFICATION=C LC_MEASUREMENT=C LC_MESSAGES=C \
116 LC_MONETARY=C LC_NAME=C LC_NUMERIC=C LC_PAPER=C \
117 LC_TELEPHONE=C LC_TIME=C
118 export LANG LC_ADDRESS LC_COLLATE LC_CTYPE \
119 LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES \
120 LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
123 # ---------------------------- Functions
126 # Function to prepare for email generation. This decides what type
127 # of update this is and whether an email should even be generated.
132 oldrev=$(git rev-parse $1)
133 newrev=$(git rev-parse $2)
137 # 0000->1234 (create)
138 # 1234->2345 (update)
139 # 2345->0000 (delete)
140 if expr "$oldrev" : '0*$' >/dev/null
144 if expr "$newrev" : '0*$' >/dev/null
152 # --- Get the revision types
153 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
154 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
155 case "$change_type" in
158 rev_type="$newrev_type"
162 rev_type="$oldrev_type"
166 # The revision type tells us what type the commit is, combined with
167 # the location of the ref we can decide between
172 case "$refname","$rev_type" in
176 short_refname=${refname##refs/tags/}
180 refname_type="annotated tag"
181 short_refname=${refname##refs/tags/}
183 if [ -n "$announcerecipients" ]; then
184 recipients="$announcerecipients"
189 refname_type="branch"
190 short_refname=${refname##refs/heads/}
192 refs/changes/*,commit)
194 refname_type="review change"
195 short_refname=${refname##refs/}
197 refs/remotes/*,commit)
199 refname_type="tracking branch"
200 short_refname=${refname##refs/remotes/}
201 echo >&2 "*** Push-update of tracking branch, $refname"
202 echo >&2 "*** - no email generated."
206 # Anything else (is there anything else?)
207 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
208 echo >&2 "*** - no email generated"
213 # Check if we've got anyone to send to
214 if [ -z "$recipients" ]; then
215 case "$refname_type" in
217 config_name="hooks.announcelist"
220 config_name="hooks.mailinglist"
223 echo >&2 "*** $config_name is not set so no email will be sent"
224 echo >&2 "*** for $refname update $oldrev->$newrev"
232 # Top level email generation function. This calls the appropriate
233 # body-generation routine after outputting the common header.
235 # Note this function doesn't actually generate any email output, that is
236 # taken care of by the functions it calls:
237 # - generate_email_header
238 # - generate_create_XXXX_email
239 # - generate_update_XXXX_email
240 # - generate_delete_XXXX_email
241 # - generate_email_footer
243 # Note also that this function cannot 'exit' from the script; when this
244 # function is running (in hook script mode), the send_mail() function
245 # is already executing in another process, connected via a pipe, and
246 # if this function exits without, whatever has been generated to that
247 # point will be sent as an email... even if nothing has been generated.
252 # The email subject will contain the best description of the ref
253 # that we can build from the parameters
254 generate_email_header
256 # Call the correct body generation function
258 case "$refname_type" in
259 "tracking branch"|branch)
267 if [ -z "$maxlines" ]; then
268 generate_${change_type}_${fn_name}_email
270 generate_${change_type}_${fn_name}_email | limit_lines $maxlines
273 generate_email_footer
276 generate_email_header()
278 # --- Email (all stdout will be the email)
281 routing_headers="To: $recipients"
282 test -n "$envelopesender" && routing_headers="From: $envelopesender${nl}$routing_headers"
283 test -n "$replyto" && routing_headers="$routing_headers${nl}Reply-To: $replyto"
285 subj="Subject: ${emailprefix}$shortdesc $refname_type $short_refname ${change_type}d. $(git show --no-patch --format='%h %s' $newrev)"
286 subj=$(print -nr -- "$subj" | php -r '
287 mb_internal_encoding("UTF-8");
288 echo mb_encode_mimeheader(file_get_contents("php://stdin"),
289 "UTF-8", "Q", "\012");')
293 Content-Type: text/plain; charset=UTF-8
294 Content-Transfer-Encoding: 8bit
296 X-Git-Refname: $refname
297 X-Git-Reftype: $refname_type
298 X-Git-Oldrev: $oldrev
299 X-Git-Newrev: $newrev
301 Auto-Submitted: auto-generated
303 This is an automated email from the git hooks/post-receive script. It was
304 generated because a ref change was pushed to the repository containing
305 the project "$projectdesc".
307 The $refname_type, $short_refname has been ${change_type}d
311 generate_email_footer()
323 # --------------- Branches
326 # Called for the creation of a branch
328 generate_create_branch_email()
330 # This is a new branch and so oldrev is not valid
331 echo " at $newrev ($newrev_type)"
340 # Called for the change of a pre-existing branch
342 generate_update_branch_email()
345 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
347 # O is $oldrev for $refname
348 # N is $newrev for $refname
349 # X is a revision pointed to by some other ref, for which we may
350 # assume that an email has already been generated.
351 # In this case we want to issue an email containing only revisions
352 # 3, 4, and N. Given (almost) by
354 # git rev-list N ^O --not --all
356 # The reason for the "almost", is that the "--not --all" will take
357 # precedence over the "N", and effectively will translate to
359 # git rev-list N ^O ^X ^N
361 # So, we need to build up the list more carefully. git rev-parse
362 # will generate a list of revs that may be fed into git rev-list.
363 # We can get it to make the "--not --all" part and then filter out
366 # git rev-parse --not --all | grep -v N
368 # Then, using the --stdin switch to git rev-list we have effectively
371 # git rev-list N ^O ^X
373 # This leaves a problem when someone else updates the repository
374 # while this script is running. Their new value of the ref we're
375 # working on would be included in the "--not --all" output; and as
376 # our $newrev would be an ancestor of that commit, it would exclude
377 # all of our commits. What we really want is to exclude the current
378 # value of $refname from the --not list, rather than N itself. So:
380 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
382 # Get's us to something pretty safe (apart from the small time
383 # between refname being read, and git rev-parse running - for that,
387 # Next problem, consider this:
388 # * --- B --- * --- O ($oldrev)
390 # * --- X --- * --- N ($newrev)
392 # That is to say, there is no guarantee that oldrev is a strict
393 # subset of newrev (it would have required a --force, but that's
394 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
395 # Instead we find the common base of the two revs and list from
398 # As above, we need to take into account the presence of X; if
399 # another branch is already in the repository and points at some of
400 # the revisions that we are about to output - we don't want them.
401 # The solution is as before: git rev-parse output filtered.
403 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
405 # Tags pushed into the repository generate nice shortlog emails that
406 # summarise the commits between them and the previous tag. However,
407 # those emails don't include the full commit messages that we output
408 # for a branch update. Therefore we still want to output revisions
409 # that have been output on a tag email.
411 # Luckily, git rev-parse includes just the tool. Instead of using
412 # "--all" we use "--branches"; this has the added benefit that
413 # "remotes/" will be ignored as well.
415 # List all of the revisions that were removed by this update, in a
416 # fast-forward update, this list will be empty, because rev-list O
417 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
421 for rev in $(git rev-list $newrev..$oldrev)
423 revtype=$(git cat-file -t "$rev")
424 echo " discards $rev ($revtype)"
426 if [ -z "$rev" ]; then
430 # List all the revisions from baserev to newrev in a kind of
431 # "table-of-contents"; note this list can include revisions that
432 # have already had notification emails and is present to show the
433 # full detail of the change from rolling back the old revision to
434 # the base revision and then forward to the new revision
435 for rev in $(git rev-list $oldrev..$newrev)
437 revtype=$(git cat-file -t "$rev")
438 echo " via $rev ($revtype)"
441 if [ "$fast_forward" ]; then
442 echo " from $oldrev ($oldrev_type)"
444 # 1. Existing revisions were removed. In this case newrev
445 # is a subset of oldrev - this is the reverse of a
446 # fast-forward, a rewind
447 # 2. New revisions were added on top of an old revision,
448 # this is a rewind and addition.
450 # (1) certainly happened, (2) possibly. When (2) hasn't
451 # happened, we set a flag to indicate that no log printout
456 # Find the common ancestor of the old and new revisions and
457 # compare it with newrev
458 baserev=$(git merge-base $oldrev $newrev)
460 if [ "$baserev" = "$newrev" ]; then
461 echo "This update discarded existing revisions and left the branch pointing at"
462 echo "a previous point in the repository history."
464 echo " * -- * -- N ($newrev)"
466 echo " O -- O -- O ($oldrev)"
468 echo "The removed revisions are not necessarily gone - if another reference"
469 echo "still refers to them they will stay in the repository."
472 echo "This update added new revisions after undoing existing revisions. That is"
473 echo "to say, the old revision is not a strict subset of the new revision. This"
474 echo "situation occurs when you --force push a change and generate a repository"
475 echo "containing something like this:"
477 echo " * -- * -- B -- O -- O -- O ($oldrev)"
479 echo " N -- N -- N ($newrev)"
481 echo "When this happens we assume that you've already had alert emails for all"
482 echo "of the O revisions, and so we here report only the revisions in the N"
483 echo "branch from the common base, B."
488 if [ -z "$rewind_only" ]; then
489 echo "Those revisions listed above that are new to this repository have"
490 echo "not appeared on any other notification email; so we list those"
491 echo "revisions in full, below."
497 # XXX: Need a way of detecting whether git rev-list actually
498 # outputted anything, so that we can issue a "no new
499 # revisions added by this update" message
503 echo "No new revisions were added by this update."
506 # The diffstat is shown from the old revision to the new revision.
507 # This is to show the truth of what happened in this change.
508 # There's no point showing the stat from the base to the new
509 # revision because the base is effectively a random revision at this
510 # point - the user will be interested in what this revision changed
511 # - including the undoing of previous revisions in the case of
512 # non-fast-forward updates.
514 echo "Summary of changes:"
515 git diff-tree $diffopts $oldrev..$newrev
519 # Called for the deletion of a branch
521 generate_delete_branch_email()
526 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
530 # --------------- Annotated tags
533 # Called for the creation of an annotated tag
535 generate_create_atag_email()
537 echo " at $newrev ($newrev_type)"
543 # Called for the update of an annotated tag (this is probably a rare event
544 # and may not even be allowed)
546 generate_update_atag_email()
548 echo " to $newrev ($newrev_type)"
549 echo " from $oldrev (which is now obsolete)"
555 # Called when an annotated tag is created or changed
557 generate_atag_email()
559 # Use git for-each-ref to pull out the individual fields from the
561 eval $(git for-each-ref --shell --format='
562 tagobject=%(*objectname)
563 tagtype=%(*objecttype)
565 tagged=%(taggerdate)' $refname
568 echo " tagging $tagobject ($tagtype)"
572 # If the tagged object is a commit, then we assume this is a
573 # release, and so we calculate which tag this tag is
575 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
577 if [ -n "$prevtag" ]; then
578 echo " replaces $prevtag"
582 echo " length $(git cat-file -s $tagobject) bytes"
585 echo " tagged by $tagger"
591 # Show the content of the tag message; this might contain a change
592 # log or release notes so is worth displaying.
593 git cat-file tag $newrev | sed -e '1,/^$/d'
598 # Only commit tags make sense to have rev-list operations
600 if [ -n "$prevtag" ]; then
601 # Show changes since the previous release
602 git shortlog --encoding=UTF-8 "$prevtag..$newrev"
604 # No previous tag, show all the changes since time
606 git shortlog --encoding=UTF-8 $newrev
610 # XXX: Is there anything useful we can do for non-commit
619 # Called for the deletion of an annotated tag
621 generate_delete_atag_email()
626 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
630 # --------------- General references
633 # Called when any other type of reference is created (most likely a
636 generate_create_general_email()
638 echo " at $newrev ($newrev_type)"
640 generate_general_email
644 # Called when any other type of reference is updated (most likely a
647 generate_update_general_email()
649 echo " to $newrev ($newrev_type)"
652 generate_general_email
656 # Called for creation or update of any other type of reference
658 generate_general_email()
660 # Unannotated tags are more about marking a point than releasing a
661 # version; therefore we don't do the shortlog summary that we do for
662 # annotated tags above - we simply show that the point has been
663 # marked, and print the log message for the marked point for
666 # Note this section also catches any other reference type (although
667 # there aren't any) and deals with them in the same way.
670 if [ "$newrev_type" = "commit" ]; then
672 git diff-tree -s --always --encoding=UTF-8 --pretty=medium $newrev
675 # What can we do here? The tag marks an object that is not
676 # a commit, so there is no log for us to display. It's
677 # probably not wise to output git cat-file as it could be a
678 # binary blob. We'll just say how big it is
679 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
684 # Called for the deletion of any other type of reference
686 generate_delete_general_email()
691 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
696 # --------------- Miscellaneous utilities
699 # Show new revisions as the user would like to see them in the email.
703 # This shows all log entries that are not already covered by
704 # another ref - i.e. commits that are now accessible from this
705 # ref that were previously not accessible
706 # (see generate_update_branch_email for the explanation of this
709 # Revision range passed to rev-list differs for new vs. updated
711 if [ "$change_type" = create ]
713 # Show all revisions exclusive to this (new) branch.
716 # Branch update; show revisions not part of $oldrev.
717 revspec=$oldrev..$newrev
720 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
722 git rev-parse --not $other_branches |
723 if [ -z "$custom_showrev" ]
725 git rev-list --pretty --stdin $revspec
727 git rev-list --stdin $revspec |
730 eval $(printf "$custom_showrev" $onerev)
740 while IFS="" read -r line; do
742 if [ $lines -gt $1 ]; then
743 skipped=$((skipped + 1))
745 printf "%s\n" "$line"
748 if [ $skipped -ne 0 ]; then
749 echo "... $skipped lines suppressed ..."
756 send_mail_x=$(cat; echo x)
758 send_mail_x=${send_mail_x%x}
759 test -n "$send_mail_x" || return 1
761 printf '%s' "$send_mail_x" | if [ -n "$envelopesender" ]; then
762 /usr/sbin/sendmail -t -f "$envelopesender"
764 /usr/sbin/sendmail -t
769 # ---------------------------- main()
772 LOGBEGIN="- Log -----------------------------------------------------------------"
773 LOGEND="-----------------------------------------------------------------------"
776 # Set GIT_DIR either from the working directory, or from the environment
778 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
779 if [ -z "$GIT_DIR" ]; then
780 echo >&2 "fatal: post-receive: GIT_DIR not set"
784 projectdesc=$(sed -ne '1p' "$GIT_DIR/description" 2>/dev/null)
785 # Check if the description is unchanged from it's default, and shorten it to
786 # a more manageable length if it is
787 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
789 projectdesc="UNNAMED PROJECT"
792 recipients=$(git config hooks.mailinglist)
793 announcerecipients=$(git config hooks.announcelist)
794 envelopesender=$(git config hooks.envelopesender)
795 emailprefix=$(git config hooks.emailprefix)
796 custom_showrev=$(git config hooks.showrev)
797 maxlines=$(git config hooks.emailmaxlines)
798 diffopts=$(git config hooks.diffopts)
799 : ${diffopts:="-p --stat --summary --find-copies-harder"}
800 kgbconffile=$(git config hooks.kgbconffile)
801 replyto=$(git config hooks.replyto)
802 shortdesc=$(git config hooks.shortdesc)
803 : ${shortdesc:=$projectdesc}
806 # Allow dual mode: run from the command line just like the update hook, or
807 # if no arguments are given then run as a hook script
808 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
809 # Output to the terminal in command line mode - if someone wanted to
810 # resend an email; they could redirect the output to sendmail
812 prep_for_email $2 $3 $1 && PAGER= generate_email
815 while read oldrev newrev refname
817 ilines="$ilines$oldrev $newrev $refname$nl"
818 prep_for_email $oldrev $newrev $refname || continue
819 generate_email $maxlines | send_mail
821 if test -n "$kgbconffile" && test -s "$kgbconffile"; then
822 printf '%s' "$ilines" | kgb-client \
823 --conf "$kgbconffile" --repository git --git-reflog -