remote: use a local variable in match_push_refs()
[git.git] / contrib / hooks / post-receive-email
blobba077c13f96779d5b29237e83974b0910b4bbdde
1 #!/bin/sh
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
24 # push.
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
31 # wanted.
33 # Config
34 # ------
35 # hooks.mailinglist
36 # This is the list that all pushes will go to; leave it blank to not send
37 # emails for every ref update.
38 # hooks.announcelist
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
45 # hooks.emailprefix
46 # All emails have their subjects prefixed with this prefix, or "[SCM]"
47 # if emailprefix is unset, to aid filtering
48 # hooks.showrev
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"
57 # or printf.
58 # hooks.emailmaxlines
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.
63 # hooks.diffopts
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
67 # summary output.
69 # Notes
70 # -----
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.
82 prep_for_email()
84 # --- Arguments
85 oldrev=$(git rev-parse $1)
86 newrev=$(git rev-parse $2)
87 refname="$3"
88 maxlines=$4
90 # --- Interpret
91 # 0000->1234 (create)
92 # 1234->2345 (update)
93 # 2345->0000 (delete)
94 if expr "$oldrev" : '0*$' >/dev/null
95 then
96 change_type="create"
97 else
98 if expr "$newrev" : '0*$' >/dev/null
99 then
100 change_type="delete"
101 else
102 change_type="update"
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
110 create|update)
111 rev="$newrev"
112 rev_type="$newrev_type"
114 delete)
115 rev="$oldrev"
116 rev_type="$oldrev_type"
118 esac
120 # The revision type tells us what type the commit is, combined with
121 # the location of the ref we can decide between
122 # - working branch
123 # - tracking branch
124 # - unannoted tag
125 # - annotated tag
126 case "$refname","$rev_type" in
127 refs/tags/*,commit)
128 # un-annotated tag
129 refname_type="tag"
130 short_refname=${refname##refs/tags/}
132 refs/tags/*,tag)
133 # annotated tag
134 refname_type="annotated tag"
135 short_refname=${refname##refs/tags/}
136 # change recipients
137 if [ -n "$announcerecipients" ]; then
138 recipients="$announcerecipients"
141 refs/heads/*,commit)
142 # branch
143 refname_type="branch"
144 short_refname=${refname##refs/heads/}
146 refs/remotes/*,commit)
147 # tracking branch
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."
152 return 1
155 # Anything else (is there anything else?)
156 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
157 echo >&2 "*** - no email generated"
158 return 1
160 esac
162 # Check if we've got anyone to send to
163 if [ -z "$recipients" ]; then
164 case "$refname_type" in
165 "annotated tag")
166 config_name="hooks.announcelist"
169 config_name="hooks.mailinglist"
171 esac
172 echo >&2 "*** $config_name is not set so no email will be sent"
173 echo >&2 "*** for $refname update $oldrev->$newrev"
174 return 1
177 return 0
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.
198 generate_email()
200 # Email parameters
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
205 describe=$rev
208 generate_email_header
210 # Call the correct body generation function
211 fn_name=general
212 case "$refname_type" in
213 "tracking branch"|branch)
214 fn_name=branch
216 "annotated tag")
217 fn_name=atag
219 esac
221 if [ -z "$maxlines" ]; then
222 generate_${change_type}_${fn_name}_email
223 else
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)
233 # Generate header
234 cat <<-EOF
235 To: $recipients
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()
252 SPACE=" "
253 cat <<-EOF
256 hooks/post-receive
257 --${SPACE}
258 $projectdesc
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)"
271 echo ""
273 echo $LOGBEGIN
274 show_new_revisions
275 echo $LOGEND
279 # Called for the change of a pre-existing branch
281 generate_update_branch_email()
283 # Consider this:
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
303 # the "^N" with:
305 # git rev-parse --not --all | grep -v N
307 # Then, using the --stdin switch to git rev-list we have effectively
308 # manufactured
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,
323 # I give up)
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
335 # there.
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
357 # revisions
358 fast_forward=""
359 rev=""
360 for rev in $(git rev-list $newrev..$oldrev)
362 revtype=$(git cat-file -t "$rev")
363 echo " discards $rev ($revtype)"
364 done
365 if [ -z "$rev" ]; then
366 fast_forward=1
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)"
378 done
380 if [ "$fast_forward" ]; then
381 echo " from $oldrev ($oldrev_type)"
382 else
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
391 # is required.
393 echo ""
395 # Find the common ancestor of the old and new revisions and
396 # compare it with newrev
397 baserev=$(git merge-base $oldrev $newrev)
398 rewind_only=""
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."
402 echo ""
403 echo " * -- * -- N ($newrev)"
404 echo " \\"
405 echo " O -- O -- O ($oldrev)"
406 echo ""
407 echo "The removed revisions are not necessarilly gone - if another reference"
408 echo "still refers to them they will stay in the repository."
409 rewind_only=1
410 else
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:"
415 echo ""
416 echo " * -- * -- B -- O -- O -- O ($oldrev)"
417 echo " \\"
418 echo " N -- N -- N ($newrev)"
419 echo ""
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."
426 echo ""
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."
432 echo ""
433 echo $LOGBEGIN
434 show_new_revisions
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
440 echo $LOGEND
441 else
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.
452 echo ""
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()
462 echo " was $oldrev"
463 echo ""
464 echo $LOGEND
465 git show -s --pretty=oneline $oldrev
466 echo $LOGEND
469 # --------------- Annotated tags
472 # Called for the creation of an annotated tag
474 generate_create_atag_email()
476 echo " at $newrev ($newrev_type)"
478 generate_atag_email
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)"
490 generate_atag_email
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
499 # tag
500 eval $(git for-each-ref --shell --format='
501 tagobject=%(*objectname)
502 tagtype=%(*objecttype)
503 tagger=%(taggername)
504 tagged=%(taggerdate)' $refname
507 echo " tagging $tagobject ($tagtype)"
508 case "$tagtype" in
509 commit)
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
513 # replacing
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"
523 esac
524 echo " tagged by $tagger"
525 echo " on $tagged"
527 echo ""
528 echo $LOGBEGIN
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'
534 echo ""
535 case "$tagtype" in
536 commit)
537 # Only commit tags make sense to have rev-list operations
538 # performed on them
539 if [ -n "$prevtag" ]; then
540 # Show changes since the previous release
541 git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
542 else
543 # No previous tag, show all the changes since time
544 # began
545 git rev-list --pretty=short $newrev | git shortlog
549 # XXX: Is there anything useful we can do for non-commit
550 # objects?
552 esac
554 echo $LOGEND
558 # Called for the deletion of an annotated tag
560 generate_delete_atag_email()
562 echo " was $oldrev"
563 echo ""
564 echo $LOGEND
565 git show -s --pretty=oneline $oldrev
566 echo $LOGEND
569 # --------------- General references
572 # Called when any other type of reference is created (most likely a
573 # non-annotated tag)
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
584 # non-annotated tag)
586 generate_update_general_email()
588 echo " to $newrev ($newrev_type)"
589 echo " from $oldrev"
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
603 # reference purposes
605 # Note this section also catches any other reference type (although
606 # there aren't any) and deals with them in the same way.
608 echo ""
609 if [ "$newrev_type" = "commit" ]; then
610 echo $LOGBEGIN
611 git show --no-color --root -s --pretty=medium $newrev
612 echo $LOGEND
613 else
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()
627 echo " was $oldrev"
628 echo ""
629 echo $LOGEND
630 git show -s --pretty=oneline $oldrev
631 echo $LOGEND
635 # --------------- Miscellaneous utilities
638 # Show new revisions as the user would like to see them in the email.
640 show_new_revisions()
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
646 # command)
648 # Revision range passed to rev-list differs for new vs. updated
649 # branches.
650 if [ "$change_type" = create ]
651 then
652 # Show all revisions exclusive to this (new) branch.
653 revspec=$newrev
654 else
655 # Branch update; show revisions not part of $oldrev.
656 revspec=$oldrev..$newrev
659 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
660 grep -F -v $refname)
661 git rev-parse --not $other_branches |
662 if [ -z "$custom_showrev" ]
663 then
664 git rev-list --pretty --stdin $revspec
665 else
666 git rev-list --stdin $revspec |
667 while read onerev
669 eval $(printf "$custom_showrev" $onerev)
670 done
675 limit_lines()
677 lines=0
678 skipped=0
679 while IFS="" read -r line; do
680 lines=$((lines + 1))
681 if [ $lines -gt $1 ]; then
682 skipped=$((skipped + 1))
683 else
684 printf "%s\n" "$line"
686 done
687 if [ $skipped -ne 0 ]; then
688 echo "... $skipped lines suppressed ..."
693 send_mail()
695 if [ -n "$envelopesender" ]; then
696 /usr/sbin/sendmail -t -f "$envelopesender"
697 else
698 /usr/sbin/sendmail -t
702 # ---------------------------- main()
704 # --- Constants
705 LOGBEGIN="- Log -----------------------------------------------------------------"
706 LOGEND="-----------------------------------------------------------------------"
708 # --- Config
709 # Set GIT_DIR either from the working directory, or from the environment
710 # variable.
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"
714 exit 1
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
721 then
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"}
734 # --- Main loop
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
740 # themselves
741 prep_for_email $2 $3 $1 && PAGER= generate_email
742 else
743 while read oldrev newrev refname
745 prep_for_email $oldrev $newrev $refname || continue
746 generate_email $maxlines | send_mail
747 done