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/doc/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/doc/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
29 # This is the list that all pushes will go to; leave it blank to not send
30 # emails for every ref update.
32 # This is the list that all pushes of annotated tags will go to. Leave it
33 # blank to default to the mailinglist field. The announce emails lists
34 # the short log summary of the changes since the last annotated tag.
35 # hooks.envelopesender
36 # If set then the -f option is passed to sendmail to allow the envelope
37 # sender address to be set
39 # All emails have their subjects prefixed with this prefix, or "[SCM]"
40 # if emailprefix is unset, to aid filtering
44 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
45 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
46 # give information for debugging.
49 # ---------------------------- Functions
52 # Top level email generation function. This decides what type of update
53 # this is and calls the appropriate body-generation routine after outputting
56 # Note this function doesn't actually generate any email output, that is
57 # taken care of by the functions it calls:
58 # - generate_email_header
59 # - generate_create_XXXX_email
60 # - generate_update_XXXX_email
61 # - generate_delete_XXXX_email
62 # - generate_email_footer
67 oldrev=$(git rev-parse $1)
68 newrev=$(git rev-parse $2)
75 if expr "$oldrev" : '0*$' >/dev/null
79 if expr "$newrev" : '0*$' >/dev/null
87 # --- Get the revision types
88 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
89 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
90 case "$change_type" in
93 rev_type="$newrev_type"
97 rev_type="$oldrev_type"
101 # The revision type tells us what type the commit is, combined with
102 # the location of the ref we can decide between
107 case "$refname","$rev_type" in
111 short_refname=${refname##refs/tags/}
115 refname_type="annotated tag"
116 short_refname=${refname##refs/tags/}
118 if [ -n "$announcerecipients" ]; then
119 recipients="$announcerecipients"
124 refname_type="branch"
125 short_refname=${refname##refs/heads/}
127 refs/remotes/*,commit)
129 refname_type="tracking branch"
130 short_refname=${refname##refs/remotes/}
131 echo >&2 "*** Push-update of tracking branch, $refname"
132 echo >&2 "*** - no email generated."
136 # Anything else (is there anything else?)
137 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
138 echo >&2 "*** - no email generated"
143 # Check if we've got anyone to send to
144 if [ -z "$recipients" ]; then
145 case "$refname_type" in
147 config_name="hooks.announcelist"
150 config_name="hooks.mailinglist"
153 echo >&2 "*** $config_name is not set so no email will be sent"
154 echo >&2 "*** for $refname update $oldrev->$newrev"
158 # The email subject will contain the best description of the ref
159 # that we can build from the parameters
162 # Call the correct body generation function
164 case "$refname_type" in
165 "tracking branch"|branch)
176 revs=$(list_${change_type}_branch_revs 2>/dev/null)
177 if [ "$(echo "$revs" | wc -l)" -gt 1 ]
182 if [ "$change_type" != "update" ]
184 set_update_branch_subject
186 generate_email_header
187 generate_${change_type}_branch_email
188 summarize_branch_revs
189 generate_email_footer
199 # set_update_branch_subject will return false if the
200 # commit is boring (no change). Then we don't even
202 set_update_branch_subject || continue
204 generate_email_header
205 print_change_info $newrev
206 summarize_branch_revs
207 generate_email_footer
214 generate_email_header
215 generate_${change_type}_${fn_name}_email
216 generate_email_footer
224 describe=$(git describe --abbrev=4 $1 2>/dev/null || git rev-parse --short $1)
225 describe=$(echo "$describe" | sed -e 's/-/./g;s/^v//;')
228 generate_email_header()
230 # --- Email (all stdout will be the email)
232 print_change_type=" ${change_type}d"
233 case "$change_type" in
239 print_refname="$refname_type "
240 case "$refname_type" in
248 Subject: ${emailprefix}$describe $print_refname$short_refname${print_change_type}$detail
249 X-Git-Refname: $refname
250 X-Git-Reftype: $refname_type
251 X-Git-Newrev: $newrev
256 generate_email_footer()
266 # --------------- Branches
269 # Called for the creation of a branch
271 generate_create_branch_email()
273 # This is a new branch and so oldrev is not valid
274 echo " at $newrev ($newrev_type)"
277 list_create_branch_revs()
279 git rev-parse --not --branches | grep -v $(git rev-parse $refname) |
280 git rev-list --reverse --stdin $newrev
284 # Called for the change of a pre-existing branch
286 generate_update_branch_email()
288 # XXX only valid for fast forwards
290 # List all the revisions from baserev to newrev in a kind of
291 # "table-of-contents"; note this list can include revisions that
292 # have already had notification emails and is present to show the
293 # full detail of the change from rolling back the old revision to
294 # the base revision and then forward to the new revision
295 for rev in $(git rev-list $oldrev..$newrev)
297 revtype=$(git cat-file -t "$rev")
298 echo " via $rev ($revtype)"
301 echo " from $oldrev ($oldrev_type)"
306 skip_diff_tree_parent()
316 set_update_branch_subject()
320 detail=$(git diff-tree -r -c --name-only $oldrev${oldrev:+..}$newrev |
321 skip_diff_tree_parent |
322 sed -E -e 's#/([^/]*)$# \1#' |
329 pos += length($1) + 1;
334 pos += length($2) + 1;
336 [ -n "$(git diff-tree -r --name-only --cc $oldrev${oldrev:+..}$newrev |
337 skip_diff_tree_parent)" ]
340 list_update_branch_revs()
342 git rev-parse --not --branches | grep -v $(git rev-parse $refname) |
343 git rev-list --reverse --stdin $oldrev..$newrev
346 summarize_branch_revs()
348 echo "Summary of changes:"
349 git diff-tree --stat --summary --find-copies-harder $oldrev${oldrev:+..}$newrev | skip_diff_tree_parent
351 if [ -n "$gitweburl" ]
354 echo "$gitweburl?p=$reponame;a=commitdiff;h=$newrev${oldrev:+;hp=}$oldrev"
360 git rev-list -n 1 --pretty $1
364 # Called for the deletion of a branch
366 generate_delete_branch_email()
371 git show -s --pretty=oneline $oldrev
375 # --------------- Annotated tags
378 # Called for the creation of an annotated tag
380 generate_create_atag_email()
382 echo " at $newrev ($newrev_type)"
388 # Called for the update of an annotated tag (this is probably a rare event
389 # and may not even be allowed)
391 generate_update_atag_email()
393 echo " to $newrev ($newrev_type)"
394 echo " from $oldrev (which is now obsolete)"
400 # Called when an annotated tag is created or changed
402 generate_atag_email()
404 # Use git for-each-ref to pull out the individual fields from the
406 eval $(git for-each-ref --shell --format='
407 tagobject=%(*objectname)
408 tagtype=%(*objecttype)
410 tagged=%(taggerdate)' $refname
413 echo " tagging $tagobject ($tagtype)"
417 # If the tagged object is a commit, then we assume this is a
418 # release, and so we calculate which tag this tag is
420 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
422 if [ -n "$prevtag" ]; then
423 echo " replaces $prevtag"
427 echo " length $(git cat-file -s $tagobject) bytes"
430 echo " tagged by $tagger"
436 # Show the content of the tag message; this might contain a change
437 # log or release notes so is worth displaying.
438 git cat-file tag $newrev | sed -e '1,/^$/d'
443 # Only commit tags make sense to have rev-list operations
445 if [ -n "$prevtag" ]; then
446 # Show changes since the previous release
447 git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
449 # No previous tag, show all the changes since time
451 git rev-list --pretty=short $newrev | git shortlog
455 # XXX: Is there anything useful we can do for non-commit
464 # Called for the deletion of an annotated tag
466 generate_delete_atag_email()
471 git show -s --pretty=oneline $oldrev
475 # --------------- General references
478 # Called when any other type of reference is created (most likely a
481 generate_create_general_email()
483 echo " at $newrev ($newrev_type)"
485 generate_general_email
489 # Called when any other type of reference is updated (most likely a
492 generate_update_general_email()
494 echo " to $newrev ($newrev_type)"
497 generate_general_email
501 # Called for creation or update of any other type of reference
503 generate_general_email()
505 # Unannotated tags are more about marking a point than releasing a
506 # version; therefore we don't do the shortlog summary that we do for
507 # annotated tags above - we simply show that the point has been
508 # marked, and print the log message for the marked point for
511 # Note this section also catches any other reference type (although
512 # there aren't any) and deals with them in the same way.
515 if [ "$newrev_type" = "commit" ]; then
517 git show --no-color --root -s --pretty=medium $newrev
520 # What can we do here? The tag marks an object that is not
521 # a commit, so there is no log for us to display. It's
522 # probably not wise to output git cat-file as it could be a
523 # binary blob. We'll just say how big it is
524 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
529 # Called for the deletion of any other type of reference
531 generate_delete_general_email()
536 git show -s --pretty=oneline $oldrev
542 if [ -n "$envelopesender" ]; then
543 /usr/sbin/sendmail -t -f "$envelopesender"
545 /usr/sbin/sendmail -t
549 # ---------------------------- main()
552 LOGBEGIN="- Log -----------------------------------------------------------------"
553 LOGEND="-----------------------------------------------------------------------"
556 # Set GIT_DIR either from the working directory, or from the environment
558 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
559 if [ -z "$GIT_DIR" ]; then
560 echo >&2 "fatal: post-receive: GIT_DIR not set"
564 projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
565 # Check if the description is unchanged from it's default, and shorten it to
566 # a more manageable length if it is
567 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
569 projectdesc="UNNAMED PROJECT"
572 recipients=$(git config hooks.mailinglist)
573 announcerecipients=$(git config hooks.announcelist)
574 envelopesender=$(git config hooks.envelopesender)
575 emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')
576 gitweburl=$(git config hooks.gitweburl)
577 reponame=$(git config hooks.reponame || basename $(realpath $GIT_DIR))
580 # Allow dual mode: run from the command line just like the update hook, or
581 # if no arguments are given then run as a hook script
582 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
583 # Output to the terminal in command line mode - if someone wanted to
584 # resend an email; they could redirect the output to sendmail
587 generate_email $2 $3 $1
589 while read oldrev newrev refname
592 generate_email $oldrev $newrev $refname