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
28 # hooks.post-receive-email.mailinglist
29 # This is the list that all pushes will go to; leave it blank to not send
30 # emails for every ref update.
31 # hooks.post-receive-email.announcelist
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.post-receive-email.envelopesender
36 # If set then the -f option is passed to sendmail to allow the envelope
37 # sender address to be set
41 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
42 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
43 # give information for debugging.
46 # ---------------------------- Functions
48 . $(dirname $0)/functions
51 # Top level email generation function. This decides what type of update
52 # this is and calls the appropriate body-generation routine after outputting
55 # Note this function doesn't actually generate any email output, that is
56 # taken care of by the functions it calls:
57 # - generate_email_header
58 # - generate_create_XXXX_email
59 # - generate_update_XXXX_email
60 # - generate_delete_XXXX_email
61 # - generate_email_footer
66 oldrev=$(git rev-parse $1)
67 newrev=$(git rev-parse $2)
74 # The revision type tells us what type the commit is, combined with
75 # the location of the ref we can decide between
80 case "$refname","$rev_type" in
85 short_refname=${refname##refs/tags/}
89 refname_type="annotated tag"
91 short_refname=${refname##refs/tags/}
93 if [ -n "$announcerecipients" ]; then
94 recipients="$announcerecipients"
101 short_refname=${refname##refs/heads/}
103 refs/remotes/*,commit)
105 refname_type="tracking branch"
106 short_refname=${refname##refs/remotes/}
107 echo >&2 "*** Push-update of tracking branch, $refname"
108 echo >&2 "*** - no email generated."
112 # Anything else (is there anything else?)
113 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
114 echo >&2 "*** - no email generated"
119 # Check if we've got anyone to send to
120 if [ -z "$recipients" ]; then
121 case "$refname_type" in
123 config_name="hooks.post-receive-email.announcelist"
126 config_name="hooks.post-receive-email.mailinglist"
129 echo >&2 "*** $config_name is not set so no email will be sent"
130 echo >&2 "*** for $refname update $oldrev->$newrev"
134 generate_email_header
135 generate_${change_type}_${function}_email
136 generate_email_footer
139 generate_email_header()
141 # --- Email (all stdout will be the email)
144 From: ${USER}@payflex.com
146 Subject: ${emailprefix} $short_refname $refname_type ${change_type}d. $describe
147 X-Git-Refname: $refname
148 X-Git-Reftype: $refname_type
149 X-Git-Oldrev: $oldrev
150 X-Git-Newrev: $newrev
152 The $refname_type, $short_refname has been ${change_type}d
156 generate_email_footer()
167 # --------------- Branches
170 # Called for the creation of a branch
172 generate_create_branch_email()
174 # This is a new branch and so oldrev is not valid
175 echo " at $newrev ($newrev_type)"
179 # This shows all log entries that are not already covered by
180 # another ref - i.e. commits that are now accessible from this
181 # ref that were previously not accessible
182 # (see generate_update_branch_email for the explanation of this
186 echo "$new_commits" | git rev-list --pretty --reverse --stdin
190 echo "Summary of changes:"
192 oldest_new=$(echo "$new_commits" | git rev-list --stdin | tail -n 1)
193 if [ "$oldest_new" != "" ] ; then
194 git diff-tree --stat -p $oldest_new^..$newrev
199 # Called for the change of a pre-existing branch
201 generate_update_branch_email()
204 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
206 # O is $oldrev for $refname
207 # N is $newrev for $refname
208 # X is a revision pointed to by some other ref, for which we may
209 # assume that an email has already been generated.
210 # In this case we want to issue an email containing only revisions
211 # 3, 4, and N. Given (almost) by
213 # git rev-list N ^O --not --all
215 # The reason for the "almost", is that the "--not --all" will take
216 # precedence over the "N", and effectively will translate to
218 # git rev-list N ^O ^X ^N
220 # So, we need to build up the list more carefully. git rev-parse
221 # will generate a list of revs that may be fed into git rev-list.
222 # We can get it to make the "--not --all" part and then filter out
225 # git rev-parse --not --all | grep -v N
227 # Then, using the --stdin switch to git rev-list we have effectively
230 # git rev-list N ^O ^X
232 # This leaves a problem when someone else updates the repository
233 # while this script is running. Their new value of the ref we're
234 # working on would be included in the "--not --all" output; and as
235 # our $newrev would be an ancestor of that commit, it would exclude
236 # all of our commits. What we really want is to exclude the current
237 # value of $refname from the --not list, rather than N itself. So:
239 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
241 # Get's us to something pretty safe (apart from the small time
242 # between refname being read, and git rev-parse running - for that,
246 # Next problem, consider this:
247 # * --- B --- * --- O ($oldrev)
249 # * --- X --- * --- N ($newrev)
251 # That is to say, there is no guarantee that oldrev is a strict
252 # subset of newrev (it would have required a --force, but that's
253 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
254 # Instead we find the common base of the two revs and list from
257 # As above, we need to take into account the presence of X; if
258 # another branch is already in the repository and points at some of
259 # the revisions that we are about to output - we don't want them.
260 # The solution is as before: git rev-parse output filtered.
262 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
264 # Tags pushed into the repository generate nice shortlog emails that
265 # summarise the commits between them and the previous tag. However,
266 # those emails don't include the full commit messages that we output
267 # for a branch update. Therefore we still want to output revisions
268 # that have been output on a tag email.
270 # Luckily, git rev-parse includes just the tool. Instead of using
271 # "--all" we use "--branches"; this has the added benefit that
272 # "remotes/" will be ignored as well.
274 # List all of the revisions that were removed by this update, in a
275 # fast forward update, this list will be empty, because rev-list O
276 # ^N is empty. For a non fast forward, O ^N is the list of removed
280 for rev in $(git rev-list $newrev..$oldrev)
282 revtype=$(git cat-file -t "$rev")
283 echo " discards $rev ($revtype)"
285 if [ -z "$rev" ]; then
289 # List all the revisions from baserev to newrev in a kind of
290 # "table-of-contents"; note this list can include revisions that
291 # have already had notification emails and is present to show the
292 # full detail of the change from rolling back the old revision to
293 # the base revision and then forward to the new revision
294 # Changed: added --first-parent to not go down merge commits
295 for rev in $(git rev-list --first-parent $oldrev..$newrev)
297 revtype=$(git cat-file -t "$rev")
298 echo " via $rev ($revtype)"
301 if [ "$fast_forward" ]; then
302 echo " from $oldrev ($oldrev_type)"
304 # 1. Existing revisions were removed. In this case newrev
305 # is a subset of oldrev - this is the reverse of a
306 # fast-forward, a rewind
307 # 2. New revisions were added on top of an old revision,
308 # this is a rewind and addition.
310 # (1) certainly happened, (2) possibly. When (2) hasn't
311 # happened, we set a flag to indicate that no log printout
316 # Find the common ancestor of the old and new revisions and
317 # compare it with newrev
318 baserev=$(git merge-base $oldrev $newrev)
320 if [ "$baserev" = "$newrev" ]; then
321 echo "This update discarded existing revisions and left the branch pointing at"
322 echo "a previous point in the repository history."
324 echo " * -- * -- N ($newrev)"
326 echo " O -- O -- O ($oldrev)"
328 echo "The removed revisions are not necessarilly gone - if another reference"
329 echo "still refers to them they will stay in the repository."
332 echo "This update added new revisions after undoing existing revisions. That is"
333 echo "to say, the old revision is not a strict subset of the new revision. This"
334 echo "situation occurs when you --force push a change and generate a repository"
335 echo "containing something like this:"
337 echo " * -- * -- B -- O -- O -- O ($oldrev)"
339 echo " N -- N -- N ($newrev)"
341 echo "When this happens we assume that you've already had alert emails for all"
342 echo "of the O revisions, and so we here report only the revisions in the N"
343 echo "branch from the common base, B."
348 if [ -z "$rewind_only" ]; then
349 echo "Those revisions listed above that are new to this repository have"
350 echo "not appeared on any other notification email; so we list those"
351 echo "revisions in full, below."
357 echo "$new_commits" | git rev-list --reverse --pretty --stdin
359 # XXX: Need a way of detecting whether git rev-list actually
360 # outputted anything, so that we can issue a "no new
361 # revisions added by this update" message
365 echo "No new revisions were added by this update."
368 # The diffstat is shown from the old revision to the new revision.
369 # This is to show the truth of what happened in this change.
370 # There's no point showing the stat from the base to the new
371 # revision because the base is effectively a random revision at this
372 # point - the user will be interested in what this revision changed
373 # - including the undoing of previous revisions in the case of
374 # non-fast forward updates.
376 echo "Summary of changes:"
377 git diff-tree --stat -p --find-copies-harder $oldrev..$newrev
381 # Called for the deletion of a branch
383 generate_delete_branch_email()
388 git show -s --pretty=oneline $oldrev
392 # --------------- Annotated tags
395 # Called for the creation of an annotated tag
397 generate_create_atag_email()
399 echo " at $newrev ($newrev_type)"
405 # Called for the update of an annotated tag (this is probably a rare event
406 # and may not even be allowed)
408 generate_update_atag_email()
410 echo " to $newrev ($newrev_type)"
411 echo " from $oldrev (which is now obsolete)"
417 # Called when an annotated tag is created or changed
419 generate_atag_email()
421 # Use git for-each-ref to pull out the individual fields from the
423 eval $(git for-each-ref --shell --format='
424 tagobject=%(*objectname)
425 tagtype=%(*objecttype)
427 tagged=%(taggerdate)' $refname
430 echo " tagging $tagobject ($tagtype)"
434 # If the tagged object is a commit, then we assume this is a
435 # release, and so we calculate which tag this tag is
437 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
439 if [ -n "$prevtag" ]; then
440 echo " replaces $prevtag"
444 echo " length $(git cat-file -s $tagobject) bytes"
447 echo " tagged by $tagger"
453 # Show the content of the tag message; this might contain a change
454 # log or release notes so is worth displaying.
455 git cat-file tag $newrev | sed -e '1,/^$/d'
460 # Only commit tags make sense to have rev-list operations
462 if [ -n "$prevtag" ]; then
463 # Show changes since the previous release
464 git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
466 # No previous tag, show all the changes since time
468 git rev-list --pretty=short $newrev | git shortlog
472 # XXX: Is there anything useful we can do for non-commit
481 # Called for the deletion of an annotated tag
483 generate_delete_atag_email()
488 git show -s --pretty=oneline $oldrev
492 # --------------- General references
495 # Called when any other type of reference is created (most likely a
498 generate_create_ltag_email()
500 echo " at $newrev ($newrev_type)"
506 # Called when any other type of reference is updated (most likely a
509 generate_update_ltag_email()
511 echo " to $newrev ($newrev_type)"
518 # Called for creation or update of any other type of reference
520 generate_ltag_email()
522 # Unannotated tags are more about marking a point than releasing a
523 # version; therefore we don't do the shortlog summary that we do for
524 # annotated tags above - we simply show that the point has been
525 # marked, and print the log message for the marked point for
528 # Note this section also catches any other reference type (although
529 # there aren't any) and deals with them in the same way.
532 if [ "$newrev_type" = "commit" ]; then
534 git show --no-color --root -s --pretty=medium $newrev
537 # What can we do here? The tag marks an object that is not
538 # a commit, so there is no log for us to display. It's
539 # probably not wise to output git cat-file as it could be a
540 # binary blob. We'll just say how big it is
541 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
546 # Called for the deletion of any other type of reference
548 generate_delete_ltag_email()
553 git show -s --pretty=oneline $oldrev
559 if [ -n "$envelopesender" ] ; then
560 /usr/sbin/sendmail -t -f "$envelopesender"
562 # /usr/sbin/sendmail -t
563 /home/BIPFS/shaberman/local/bin/msmtp -t
567 # ---------------------------- main()
570 LOGBEGIN="- Log -----------------------------------------------------------------"
571 LOGEND="-----------------------------------------------------------------------"
574 # Set GIT_DIR either from the working directory, or from the environment
576 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
577 if [ -z "$GIT_DIR" ]; then
578 echo >&2 "fatal: post-receive: GIT_DIR not set"
582 projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
583 # Check if the description is unchanged from it's default, and shorten it to
584 # a more manageable length if it is
585 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
587 projectdesc="UNNAMED PROJECT"
590 recipients=$(git config hooks.post-receive-email.mailinglist)
591 announcerecipients=$(git config hooks.post-receive-email.announcelist)
592 envelopesender=$(git config hooks.post-receive-email.envelopesender)
593 emailprefix="[$projectdesc]"
594 debug=$(git config hooks.post-receive-email.debug)
597 # Allow dual mode: run from the command line just like the update hook, or
598 # if no arguments are given then run as a hook script
599 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
600 # Output to the terminal in command line mode - if someone wanted to
601 # resend an email; they could redirect the output to sendmail
603 PAGER= generate_email $2 $3 $1
605 while read oldrev newrev refname
607 if [ "$debug" == "true" ] ; then
608 generate_email $oldrev $newrev $refname > "${refname//\//.}.out"
610 generate_email $oldrev $newrev $refname | send_mail