4 # Thorsten “mirabilos” Glaser <thorsten.glaser@teckids.org>
5 # Copyright © 2011, 2012, 2013, 2014, 2015
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 # ln -sf /etc/gforge/plugins/scmgit/post-receive-email.sh hooks/post-receive
40 # # but that can only be used for a Forge-controlled repository
42 # This hook script needs the “en_US.UTF-8” locale set up on the server.
43 # The MirBSD Korn Shell (mksh) must be installed as /bin/mksh and usable.
45 # This hook script assumes it is enabled on the central repository of a
46 # project, with all users pushing only to it and not between each other. It
47 # will still work if you don't operate in that style, but it would become
48 # possible for the email to be from someone other than the person doing the
51 # To help with debugging and use on pre-v1.5.1 git servers, this script will
52 # also obey the interface of hooks/update, taking its arguments on the
53 # command line. Unfortunately, hooks/update is called once for each ref.
54 # To avoid firing one email per ref, this script just prints its output to
55 # the screen when used in this mode. The output can then be redirected if
61 # This is the list that all pushes will go to; leave it blank to not send
62 # emails for every ref update.
64 # This is the list that all pushes of annotated tags will go to. Leave it
65 # blank to default to the mailinglist field. The announce emails lists
66 # the short log summary of the changes since the last annotated tag.
68 # If set, replies are requested to be directed to this eMail address.
69 # hooks.envelopesender
70 # If set then the -f option is passed to sendmail to allow the envelope
71 # sender address to be set. This is also used as Header From.
73 # If set, e.g. to '[SCM] ', all eMails have their subjects prefixed
74 # with this string to aid filtering.
76 # If set, this is used in the Subject instead of the description file.
78 # The shell command used to format each revision in the email, with
79 # "%s" replaced with the commit id. Defaults to "git rev-list -1
80 # --pretty %s", displaying the commit id, author, date and log
81 # message. To list full patches separated by a blank line, you
82 # could set this to "git show -C %s; echo". If you do that, make
83 # sure to remove the "-p" from hooks.diffopts to avoid getting
84 # two sets of diffs (the showrev diff is per commit, whereas the
85 # diffopts patch will show what really happened in the push as
87 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
88 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
89 # Be careful if "..." contains things that will be expanded by shell "eval"
92 # The maximum number of lines that should be included in the generated
93 # email body. If not specified, there is no limit.
94 # Lines beyond the limit are suppressed and counted, and a final
95 # line is added indicating the number of suppressed lines.
97 # Alternate options for the git diff-tree invocation that shows changes.
98 # Default is "-p --stat --summary --find-copies-harder". Remove -p from
99 # those options to skil including a unified diff of changes in addition
100 # to the summary output. See hooks.showrev for more discussion on this.
102 # Full path to a configuration file for kgb-client, or unset/empty.
103 # If set, kgb-client will be invoked.
105 # List (expanded by shell “for”, so be careful) of URIs to trigger on
106 # each push to be accessed with cURL.
110 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
111 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
112 # give information for debugging.
115 # ---------------------------- Environment Sanitisation
119 unset LANGUAGE LC_ALL
120 export LANG=C LC_ADDRESS=C LC_COLLATE=C LC_CTYPE=en_US.UTF-8 \
121 LC_IDENTIFICATION=C LC_MEASUREMENT=C LC_MESSAGES=C \
122 LC_MONETARY=C LC_NAME=C LC_NUMERIC=C LC_PAPER=C \
123 LC_TELEPHONE=C LC_TIME=C
125 # ---------------------------- Functions
128 # Function to prepare for email generation. This decides what type
129 # of update this is and whether an email should even be generated.
134 oldrev=$(git rev-parse $1)
135 newrev=$(git rev-parse $2)
139 # 0000->1234 (create)
140 # 1234->2345 (update)
141 # 2345->0000 (delete)
142 if expr "$oldrev" : '0*$' >/dev/null
146 if expr "$newrev" : '0*$' >/dev/null
154 # --- Get the revision types
155 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
156 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
157 case "$change_type" in
160 rev_type="$newrev_type"
164 rev_type="$oldrev_type"
168 # The revision type tells us what type the commit is, combined with
169 # the location of the ref we can decide between
174 case "$refname","$rev_type" in
178 short_refname=${refname##refs/tags/}
182 refname_type="annotated tag"
183 short_refname=${refname##refs/tags/}
185 if [ -n "$announcerecipients" ]; then
186 recipients="$announcerecipients"
191 refname_type="branch"
192 short_refname=${refname##refs/heads/}
194 refs/changes/*,commit)
196 refname_type="review change"
197 short_refname=${refname##refs/}
199 refs/remotes/*,commit)
201 refname_type="tracking branch"
202 short_refname=${refname##refs/remotes/}
203 echo >&2 "*** Push-update of tracking branch, $refname"
204 echo >&2 "*** - no email generated."
208 # Anything else (is there anything else?)
209 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
210 echo >&2 "*** - no email generated"
215 # Check if we've got anyone to send to
216 if [ -z "$recipients" ]; then
217 case "$refname_type" in
219 config_name="hooks.announcelist"
222 config_name="hooks.mailinglist"
225 echo >&2 "*** $config_name is not set so no email will be sent"
226 echo >&2 "*** for $refname update $oldrev->$newrev"
234 # Top level email generation function. This calls the appropriate
235 # body-generation routine after outputting the common header.
237 # Note this function doesn't actually generate any email output, that is
238 # taken care of by the functions it calls:
239 # - generate_email_header
240 # - generate_create_XXXX_email
241 # - generate_update_XXXX_email
242 # - generate_delete_XXXX_email
243 # - generate_email_footer
245 # Note also that this function cannot 'exit' from the script; when this
246 # function is running (in hook script mode), the send_mail() function
247 # is already executing in another process, connected via a pipe, and
248 # if this function exits without, whatever has been generated to that
249 # point will be sent as an email... even if nothing has been generated.
254 # The email subject will contain the best description of the ref
255 # that we can build from the parameters
256 describe=$(git describe $rev 2>/dev/null)
257 if [ -z "$describe" ]; then
261 generate_email_header
263 # Call the correct body generation function
265 case "$refname_type" in
266 "tracking branch"|branch)
274 if [ -z "$maxlines" ]; then
275 generate_${change_type}_${fn_name}_email
277 generate_${change_type}_${fn_name}_email | limit_lines $maxlines
280 generate_email_footer
283 generate_email_header()
285 local routing_headers subj
287 # --- Email (all stdout will be the email)
290 routing_headers="To: $recipients"
291 [[ -n $envelopesender ]] && routing_headers="From: $envelopesender${nl}$routing_headers"
292 [[ -n $replyto ]] && routing_headers="$routing_headers${nl}Reply-To: $replyto"
294 subj="Subject: ${emailprefix}$shortdesc $refname_type $short_refname ${change_type}d. $describe"
295 subj=$(print -nr -- "$subj" | php -r '
296 mb_internal_encoding("UTF-8");
297 echo mb_encode_mimeheader(file_get_contents("php://stdin"),
298 "UTF-8", "Q", "\012");')
300 if [[ -z $subj ]]; then
301 print -u2 "E: Subject empty, RFC2047 MIME encoding failed"
307 Content-Type: text/plain; charset=UTF-8
308 Content-Transfer-Encoding: 8bit
310 X-Git-Refname: $refname
311 X-Git-Reftype: $refname_type
312 X-Git-Oldrev: $oldrev
313 X-Git-Newrev: $newrev
315 Auto-Submitted: auto-generated
317 This is an automated email from the git hooks/post-receive script. It was
318 generated because a ref change was pushed to the repository containing
319 the project "$projectdesc".
321 The $refname_type, $short_refname has been ${change_type}d
325 generate_email_footer()
337 # --------------- Branches
340 # Called for the creation of a branch
342 generate_create_branch_email()
344 # This is a new branch and so oldrev is not valid
345 echo " at $newrev ($newrev_type)"
354 # Called for the change of a pre-existing branch
356 generate_update_branch_email()
359 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
361 # O is $oldrev for $refname
362 # N is $newrev for $refname
363 # X is a revision pointed to by some other ref, for which we may
364 # assume that an email has already been generated.
365 # In this case we want to issue an email containing only revisions
366 # 3, 4, and N. Given (almost) by
368 # git rev-list N ^O --not --all
370 # The reason for the "almost", is that the "--not --all" will take
371 # precedence over the "N", and effectively will translate to
373 # git rev-list N ^O ^X ^N
375 # So, we need to build up the list more carefully. git rev-parse
376 # will generate a list of revs that may be fed into git rev-list.
377 # We can get it to make the "--not --all" part and then filter out
380 # git rev-parse --not --all | grep -v N
382 # Then, using the --stdin switch to git rev-list we have effectively
385 # git rev-list N ^O ^X
387 # This leaves a problem when someone else updates the repository
388 # while this script is running. Their new value of the ref we're
389 # working on would be included in the "--not --all" output; and as
390 # our $newrev would be an ancestor of that commit, it would exclude
391 # all of our commits. What we really want is to exclude the current
392 # value of $refname from the --not list, rather than N itself. So:
394 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
396 # Get's us to something pretty safe (apart from the small time
397 # between refname being read, and git rev-parse running - for that,
401 # Next problem, consider this:
402 # * --- B --- * --- O ($oldrev)
404 # * --- X --- * --- N ($newrev)
406 # That is to say, there is no guarantee that oldrev is a strict
407 # subset of newrev (it would have required a --force, but that's
408 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
409 # Instead we find the common base of the two revs and list from
412 # As above, we need to take into account the presence of X; if
413 # another branch is already in the repository and points at some of
414 # the revisions that we are about to output - we don't want them.
415 # The solution is as before: git rev-parse output filtered.
417 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
419 # Tags pushed into the repository generate nice shortlog emails that
420 # summarise the commits between them and the previous tag. However,
421 # those emails don't include the full commit messages that we output
422 # for a branch update. Therefore we still want to output revisions
423 # that have been output on a tag email.
425 # Luckily, git rev-parse includes just the tool. Instead of using
426 # "--all" we use "--branches"; this has the added benefit that
427 # "remotes/" will be ignored as well.
429 # List all of the revisions that were removed by this update, in a
430 # fast-forward update, this list will be empty, because rev-list O
431 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
435 for rev in $(git rev-list $newrev..$oldrev)
437 revtype=$(git cat-file -t "$rev")
438 echo " discards $rev ($revtype)"
440 if [ -z "$rev" ]; then
444 # List all the revisions from baserev to newrev in a kind of
445 # "table-of-contents"; note this list can include revisions that
446 # have already had notification emails and is present to show the
447 # full detail of the change from rolling back the old revision to
448 # the base revision and then forward to the new revision
449 for rev in $(git rev-list $oldrev..$newrev)
451 revtype=$(git cat-file -t "$rev")
452 echo " via $rev ($revtype)"
455 if [ "$fast_forward" ]; then
456 echo " from $oldrev ($oldrev_type)"
458 # 1. Existing revisions were removed. In this case newrev
459 # is a subset of oldrev - this is the reverse of a
460 # fast-forward, a rewind
461 # 2. New revisions were added on top of an old revision,
462 # this is a rewind and addition.
464 # (1) certainly happened, (2) possibly. When (2) hasn't
465 # happened, we set a flag to indicate that no log printout
470 # Find the common ancestor of the old and new revisions and
471 # compare it with newrev
472 baserev=$(git merge-base $oldrev $newrev)
474 if [ "$baserev" = "$newrev" ]; then
475 echo "This update discarded existing revisions and left the branch pointing at"
476 echo "a previous point in the repository history."
478 echo " * -- * -- N ($newrev)"
480 echo " O -- O -- O ($oldrev)"
482 echo "The removed revisions are not necessarily gone - if another reference"
483 echo "still refers to them they will stay in the repository."
486 echo "This update added new revisions after undoing existing revisions. That is"
487 echo "to say, the old revision is not a strict subset of the new revision. This"
488 echo "situation occurs when you --force push a change and generate a repository"
489 echo "containing something like this:"
491 echo " * -- * -- B -- O -- O -- O ($oldrev)"
493 echo " N -- N -- N ($newrev)"
495 echo "When this happens we assume that you've already had alert emails for all"
496 echo "of the O revisions, and so we here report only the revisions in the N"
497 echo "branch from the common base, B."
502 if [ -z "$rewind_only" ]; then
503 echo "Those revisions listed above that are new to this repository have"
504 echo "not appeared on any other notification email; so we list those"
505 echo "revisions in full, below."
511 # XXX: Need a way of detecting whether git rev-list actually
512 # outputted anything, so that we can issue a "no new
513 # revisions added by this update" message
517 echo "No new revisions were added by this update."
520 # The diffstat is shown from the old revision to the new revision.
521 # This is to show the truth of what happened in this change.
522 # There's no point showing the stat from the base to the new
523 # revision because the base is effectively a random revision at this
524 # point - the user will be interested in what this revision changed
525 # - including the undoing of previous revisions in the case of
526 # non-fast-forward updates.
528 echo "Summary of changes:"
529 git diff-tree $diffopts $oldrev..$newrev
533 # Called for the deletion of a branch
535 generate_delete_branch_email()
540 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
544 # --------------- Annotated tags
547 # Called for the creation of an annotated tag
549 generate_create_atag_email()
551 echo " at $newrev ($newrev_type)"
557 # Called for the update of an annotated tag (this is probably a rare event
558 # and may not even be allowed)
560 generate_update_atag_email()
562 echo " to $newrev ($newrev_type)"
563 echo " from $oldrev (which is now obsolete)"
569 # Called when an annotated tag is created or changed
571 generate_atag_email()
573 # Use git for-each-ref to pull out the individual fields from the
575 eval $(git for-each-ref --shell --format='
576 tagobject=%(*objectname)
577 tagtype=%(*objecttype)
579 tagged=%(taggerdate)' $refname
582 echo " tagging $tagobject ($tagtype)"
586 # If the tagged object is a commit, then we assume this is a
587 # release, and so we calculate which tag this tag is
589 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
591 if [ -n "$prevtag" ]; then
592 echo " replaces $prevtag"
596 echo " length $(git cat-file -s $tagobject) bytes"
599 echo " tagged by $tagger"
605 # Show the content of the tag message; this might contain a change
606 # log or release notes so is worth displaying.
607 git cat-file tag $newrev | sed -e '1,/^$/d'
612 # Only commit tags make sense to have rev-list operations
614 if [ -n "$prevtag" ]; then
615 # Show changes since the previous release
616 git shortlog --encoding=UTF-8 "$prevtag..$newrev"
618 # No previous tag, show all the changes since time
620 git shortlog --encoding=UTF-8 $newrev
624 # XXX: Is there anything useful we can do for non-commit
633 # Called for the deletion of an annotated tag
635 generate_delete_atag_email()
640 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
644 # --------------- General references
647 # Called when any other type of reference is created (most likely a
650 generate_create_general_email()
652 echo " at $newrev ($newrev_type)"
654 generate_general_email
658 # Called when any other type of reference is updated (most likely a
661 generate_update_general_email()
663 echo " to $newrev ($newrev_type)"
666 generate_general_email
670 # Called for creation or update of any other type of reference
672 generate_general_email()
674 # Unannotated tags are more about marking a point than releasing a
675 # version; therefore we don't do the shortlog summary that we do for
676 # annotated tags above - we simply show that the point has been
677 # marked, and print the log message for the marked point for
680 # Note this section also catches any other reference type (although
681 # there aren't any) and deals with them in the same way.
684 if [ "$newrev_type" = "commit" ]; then
686 git diff-tree -s --always --encoding=UTF-8 --pretty=medium $newrev
689 # What can we do here? The tag marks an object that is not
690 # a commit, so there is no log for us to display. It's
691 # probably not wise to output git cat-file as it could be a
692 # binary blob. We'll just say how big it is
693 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
698 # Called for the deletion of any other type of reference
700 generate_delete_general_email()
705 git diff-tree -s --always --encoding=UTF-8 --pretty=oneline $oldrev
710 # --------------- Miscellaneous utilities
713 # Show new revisions as the user would like to see them in the email.
717 # This shows all log entries that are not already covered by
718 # another ref - i.e. commits that are now accessible from this
719 # ref that were previously not accessible
720 # (see generate_update_branch_email for the explanation of this
723 # Revision range passed to rev-list differs for new vs. updated
725 if [ "$change_type" = create ]
727 # Show all revisions exclusive to this (new) branch.
730 # Branch update; show revisions not part of $oldrev.
731 revspec=$oldrev..$newrev
734 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
736 git rev-parse --not $other_branches |
737 if [ -z "$custom_showrev" ]
739 git rev-list --pretty --stdin $revspec
741 git rev-list --stdin $revspec |
744 eval $(printf "$custom_showrev" $onerev)
754 while IFS="" read -r line; do
756 if [ $lines -gt $1 ]; then
757 skipped=$((skipped + 1))
759 printf "%s\n" "$line"
762 if [ $skipped -ne 0 ]; then
763 echo "... $skipped lines suppressed ..."
770 local x=$(cat; echo x)
773 [[ -n $x ]] || return 1
775 print -nr -- "$x" | if [ -n "$envelopesender" ]; then
776 /usr/sbin/sendmail -t -f "$envelopesender"
778 /usr/sbin/sendmail -t
782 # ---------------------------- main()
785 LOGBEGIN="- Log -----------------------------------------------------------------"
786 LOGEND="-----------------------------------------------------------------------"
789 # Set GIT_DIR either from the working directory, or from the environment
791 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
792 if [ -z "$GIT_DIR" ]; then
793 echo >&2 "fatal: post-receive: GIT_DIR not set"
797 projectdesc=$(sed -ne '1p' "$GIT_DIR/description" 2>/dev/null)
798 # Check if the description is unchanged from it's default, and shorten it to
799 # a more manageable length if it is
800 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
802 projectdesc="UNNAMED PROJECT"
805 recipients=$(git config hooks.mailinglist)
806 announcerecipients=$(git config hooks.announcelist)
807 envelopesender=$(git config hooks.envelopesender)
808 emailprefix=$(git config hooks.emailprefix)
809 custom_showrev=$(git config hooks.showrev)
810 maxlines=$(git config hooks.emailmaxlines)
811 diffopts=$(git config hooks.diffopts)
812 : "${diffopts:="-p --stat --summary --find-copies-harder"}"
813 kgbconffile=$(git config hooks.kgbconffile)
814 replyto=$(git config hooks.replyto)
815 shortdesc=$(git config hooks.shortdesc)
816 : "${shortdesc:=$projectdesc}"
819 # Allow dual mode: run from the command line just like the update hook, or
820 # if no arguments are given then run as a hook script
821 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
822 # Output to the terminal in command line mode - if someone wanted to
823 # resend an email; they could redirect the output to sendmail
825 prep_for_email $2 $3 $1 && PAGER= generate_email
828 while read oldrev newrev refname
830 ilines="$ilines$oldrev $newrev $refname$nl"
831 prep_for_email $oldrev $newrev $refname || continue
832 generate_email $maxlines | send_mail
834 if [[ -n $kgbconffile && -s $kgbconffile ]]; then
835 print -nr -- "$ilines" | kgb-client \
836 --conf "$kgbconffile" --repository git --git-reflog -
839 for uri in $(git config hooks.curltrigger); do
840 print -r -- "‣‣‣ $uri"