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 describe=$(git describe $rev 2>/dev/null)
255 if [ -z "$describe" ]; then
259 generate_email_header
261 # Call the correct body generation function
263 case "$refname_type" in
264 "tracking branch"|branch)
272 if [ -z "$maxlines" ]; then
273 generate_${change_type}_${fn_name}_email
275 generate_${change_type}_${fn_name}_email | limit_lines $maxlines
278 generate_email_footer
281 generate_email_header()
283 # --- Email (all stdout will be the email)
286 routing_headers="To: $recipients"
287 test -n "$envelopesender" && routing_headers="From: $envelopesender${nl}$routing_headers"
288 test -n "$replyto" && routing_headers="$routing_headers${nl}Reply-To: $replyto"
292 Content-Type: text/plain; charset=UTF-8
293 Content-Transfer-Encoding: 8bit
295 X-Git-Refname: $refname
296 X-Git-Reftype: $refname_type
297 X-Git-Oldrev: $oldrev
298 X-Git-Newrev: $newrev
299 $(perl -Mutf8 -MEncode -e "print encode('MIME-Q', 'Subject: ${emailprefix}$shortdesc $refname_type $short_refname ${change_type}d. $describe');")
300 Auto-Submitted: auto-generated
302 This is an automated email from the git hooks/post-receive script. It was
303 generated because a ref change was pushed to the repository containing
304 the project "$projectdesc".
306 The $refname_type, $short_refname has been ${change_type}d
310 generate_email_footer()
322 # --------------- Branches
325 # Called for the creation of a branch
327 generate_create_branch_email()
329 # This is a new branch and so oldrev is not valid
330 echo " at $newrev ($newrev_type)"
339 # Called for the change of a pre-existing branch
341 generate_update_branch_email()
344 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
346 # O is $oldrev for $refname
347 # N is $newrev for $refname
348 # X is a revision pointed to by some other ref, for which we may
349 # assume that an email has already been generated.
350 # In this case we want to issue an email containing only revisions
351 # 3, 4, and N. Given (almost) by
353 # git rev-list N ^O --not --all
355 # The reason for the "almost", is that the "--not --all" will take
356 # precedence over the "N", and effectively will translate to
358 # git rev-list N ^O ^X ^N
360 # So, we need to build up the list more carefully. git rev-parse
361 # will generate a list of revs that may be fed into git rev-list.
362 # We can get it to make the "--not --all" part and then filter out
365 # git rev-parse --not --all | grep -v N
367 # Then, using the --stdin switch to git rev-list we have effectively
370 # git rev-list N ^O ^X
372 # This leaves a problem when someone else updates the repository
373 # while this script is running. Their new value of the ref we're
374 # working on would be included in the "--not --all" output; and as
375 # our $newrev would be an ancestor of that commit, it would exclude
376 # all of our commits. What we really want is to exclude the current
377 # value of $refname from the --not list, rather than N itself. So:
379 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
381 # Get's us to something pretty safe (apart from the small time
382 # between refname being read, and git rev-parse running - for that,
386 # Next problem, consider this:
387 # * --- B --- * --- O ($oldrev)
389 # * --- X --- * --- N ($newrev)
391 # That is to say, there is no guarantee that oldrev is a strict
392 # subset of newrev (it would have required a --force, but that's
393 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
394 # Instead we find the common base of the two revs and list from
397 # As above, we need to take into account the presence of X; if
398 # another branch is already in the repository and points at some of
399 # the revisions that we are about to output - we don't want them.
400 # The solution is as before: git rev-parse output filtered.
402 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
404 # Tags pushed into the repository generate nice shortlog emails that
405 # summarise the commits between them and the previous tag. However,
406 # those emails don't include the full commit messages that we output
407 # for a branch update. Therefore we still want to output revisions
408 # that have been output on a tag email.
410 # Luckily, git rev-parse includes just the tool. Instead of using
411 # "--all" we use "--branches"; this has the added benefit that
412 # "remotes/" will be ignored as well.
414 # List all of the revisions that were removed by this update, in a
415 # fast-forward update, this list will be empty, because rev-list O
416 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
420 for rev in $(git rev-list $newrev..$oldrev)
422 revtype=$(git cat-file -t "$rev")
423 echo " discards $rev ($revtype)"
425 if [ -z "$rev" ]; then
429 # List all the revisions from baserev to newrev in a kind of
430 # "table-of-contents"; note this list can include revisions that
431 # have already had notification emails and is present to show the
432 # full detail of the change from rolling back the old revision to
433 # the base revision and then forward to the new revision
434 for rev in $(git rev-list $oldrev..$newrev)
436 revtype=$(git cat-file -t "$rev")
437 echo " via $rev ($revtype)"
440 if [ "$fast_forward" ]; then
441 echo " from $oldrev ($oldrev_type)"
443 # 1. Existing revisions were removed. In this case newrev
444 # is a subset of oldrev - this is the reverse of a
445 # fast-forward, a rewind
446 # 2. New revisions were added on top of an old revision,
447 # this is a rewind and addition.
449 # (1) certainly happened, (2) possibly. When (2) hasn't
450 # happened, we set a flag to indicate that no log printout
455 # Find the common ancestor of the old and new revisions and
456 # compare it with newrev
457 baserev=$(git merge-base $oldrev $newrev)
459 if [ "$baserev" = "$newrev" ]; then
460 echo "This update discarded existing revisions and left the branch pointing at"
461 echo "a previous point in the repository history."
463 echo " * -- * -- N ($newrev)"
465 echo " O -- O -- O ($oldrev)"
467 echo "The removed revisions are not necessarily gone - if another reference"
468 echo "still refers to them they will stay in the repository."
471 echo "This update added new revisions after undoing existing revisions. That is"
472 echo "to say, the old revision is not a strict subset of the new revision. This"
473 echo "situation occurs when you --force push a change and generate a repository"
474 echo "containing something like this:"
476 echo " * -- * -- B -- O -- O -- O ($oldrev)"
478 echo " N -- N -- N ($newrev)"
480 echo "When this happens we assume that you've already had alert emails for all"
481 echo "of the O revisions, and so we here report only the revisions in the N"
482 echo "branch from the common base, B."
487 if [ -z "$rewind_only" ]; then
488 echo "Those revisions listed above that are new to this repository have"
489 echo "not appeared on any other notification email; so we list those"
490 echo "revisions in full, below."
496 # XXX: Need a way of detecting whether git rev-list actually
497 # outputted anything, so that we can issue a "no new
498 # revisions added by this update" message
502 echo "No new revisions were added by this update."
505 # The diffstat is shown from the old revision to the new revision.
506 # This is to show the truth of what happened in this change.
507 # There's no point showing the stat from the base to the new
508 # revision because the base is effectively a random revision at this
509 # point - the user will be interested in what this revision changed
510 # - including the undoing of previous revisions in the case of
511 # non-fast-forward updates.
513 echo "Summary of changes:"
514 git diff-tree $diffopts $oldrev..$newrev
518 # Called for the deletion of a branch
520 generate_delete_branch_email()
525 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
529 # --------------- Annotated tags
532 # Called for the creation of an annotated tag
534 generate_create_atag_email()
536 echo " at $newrev ($newrev_type)"
542 # Called for the update of an annotated tag (this is probably a rare event
543 # and may not even be allowed)
545 generate_update_atag_email()
547 echo " to $newrev ($newrev_type)"
548 echo " from $oldrev (which is now obsolete)"
554 # Called when an annotated tag is created or changed
556 generate_atag_email()
558 # Use git for-each-ref to pull out the individual fields from the
560 eval $(git for-each-ref --shell --format='
561 tagobject=%(*objectname)
562 tagtype=%(*objecttype)
564 tagged=%(taggerdate)' $refname
567 echo " tagging $tagobject ($tagtype)"
571 # If the tagged object is a commit, then we assume this is a
572 # release, and so we calculate which tag this tag is
574 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
576 if [ -n "$prevtag" ]; then
577 echo " replaces $prevtag"
581 echo " length $(git cat-file -s $tagobject) bytes"
584 echo " tagged by $tagger"
590 # Show the content of the tag message; this might contain a change
591 # log or release notes so is worth displaying.
592 git cat-file tag $newrev | sed -e '1,/^$/d'
597 # Only commit tags make sense to have rev-list operations
599 if [ -n "$prevtag" ]; then
600 # Show changes since the previous release
601 git shortlog --encoding=UTF-8 "$prevtag..$newrev"
603 # No previous tag, show all the changes since time
605 git shortlog --encoding=UTF-8 $newrev
609 # XXX: Is there anything useful we can do for non-commit
618 # Called for the deletion of an annotated tag
620 generate_delete_atag_email()
625 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
629 # --------------- General references
632 # Called when any other type of reference is created (most likely a
635 generate_create_general_email()
637 echo " at $newrev ($newrev_type)"
639 generate_general_email
643 # Called when any other type of reference is updated (most likely a
646 generate_update_general_email()
648 echo " to $newrev ($newrev_type)"
651 generate_general_email
655 # Called for creation or update of any other type of reference
657 generate_general_email()
659 # Unannotated tags are more about marking a point than releasing a
660 # version; therefore we don't do the shortlog summary that we do for
661 # annotated tags above - we simply show that the point has been
662 # marked, and print the log message for the marked point for
665 # Note this section also catches any other reference type (although
666 # there aren't any) and deals with them in the same way.
669 if [ "$newrev_type" = "commit" ]; then
671 git diff-tree -s --always --encoding=UTF-8 --pretty=medium $newrev
674 # What can we do here? The tag marks an object that is not
675 # a commit, so there is no log for us to display. It's
676 # probably not wise to output git cat-file as it could be a
677 # binary blob. We'll just say how big it is
678 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
683 # Called for the deletion of any other type of reference
685 generate_delete_general_email()
690 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
695 # --------------- Miscellaneous utilities
698 # Show new revisions as the user would like to see them in the email.
702 # This shows all log entries that are not already covered by
703 # another ref - i.e. commits that are now accessible from this
704 # ref that were previously not accessible
705 # (see generate_update_branch_email for the explanation of this
708 # Revision range passed to rev-list differs for new vs. updated
710 if [ "$change_type" = create ]
712 # Show all revisions exclusive to this (new) branch.
715 # Branch update; show revisions not part of $oldrev.
716 revspec=$oldrev..$newrev
719 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
721 git rev-parse --not $other_branches |
722 if [ -z "$custom_showrev" ]
724 git rev-list --pretty --stdin $revspec
726 git rev-list --stdin $revspec |
729 eval $(printf "$custom_showrev" $onerev)
739 while IFS="" read -r line; do
741 if [ $lines -gt $1 ]; then
742 skipped=$((skipped + 1))
744 printf "%s\n" "$line"
747 if [ $skipped -ne 0 ]; then
748 echo "... $skipped lines suppressed ..."
755 send_mail_x=$(cat; echo x)
757 send_mail_x=${send_mail_x%x}
758 test -n "$send_mail_x" || return 1
760 printf '%s' "$send_mail_x" | if [ -n "$envelopesender" ]; then
761 /usr/sbin/sendmail -t -f "$envelopesender"
763 /usr/sbin/sendmail -t
768 # ---------------------------- main()
771 LOGBEGIN="- Log -----------------------------------------------------------------"
772 LOGEND="-----------------------------------------------------------------------"
775 # Set GIT_DIR either from the working directory, or from the environment
777 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
778 if [ -z "$GIT_DIR" ]; then
779 echo >&2 "fatal: post-receive: GIT_DIR not set"
783 projectdesc=$(sed -ne '1p' "$GIT_DIR/description" 2>/dev/null)
784 # Check if the description is unchanged from it's default, and shorten it to
785 # a more manageable length if it is
786 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
788 projectdesc="UNNAMED PROJECT"
791 recipients=$(git config hooks.mailinglist)
792 announcerecipients=$(git config hooks.announcelist)
793 envelopesender=$(git config hooks.envelopesender)
794 emailprefix=$(git config hooks.emailprefix)
795 custom_showrev=$(git config hooks.showrev)
796 maxlines=$(git config hooks.emailmaxlines)
797 diffopts=$(git config hooks.diffopts)
798 : ${diffopts:="-p --stat --summary --find-copies-harder"}
799 kgbconffile=$(git config hooks.kgbconffile)
800 replyto=$(git config hooks.replyto)
801 shortdesc=$(git config hooks.shortdesc)
802 : ${shortdesc:=$projectdesc}
805 # Allow dual mode: run from the command line just like the update hook, or
806 # if no arguments are given then run as a hook script
807 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
808 # Output to the terminal in command line mode - if someone wanted to
809 # resend an email; they could redirect the output to sendmail
811 prep_for_email $2 $3 $1 && PAGER= generate_email
814 while read oldrev newrev refname
816 ilines="$ilines$oldrev $newrev $refname$nl"
817 prep_for_email $oldrev $newrev $refname || continue
818 generate_email $maxlines | send_mail
820 if test -n "$kgbconffile" && test -s "$kgbconffile"; then
821 printf '%s' "$ilines" | kgb-client \
822 --conf "$kgbconffile" --repository git --git-reflog -