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
65 oldrev=$(git rev-parse $1)
66 newrev=$(git rev-parse $2)
73 # The revision type tells us what type the commit is, combined with
74 # the location of the ref we can decide between
79 case "$refname","$rev_type" in
84 short_refname=${refname##refs/tags/}
88 refname_type="annotated tag"
90 short_refname=${refname##refs/tags/}
92 if [ -n "$announcerecipients" ]; then
93 recipients="$announcerecipients"
100 short_refname=${refname##refs/heads/}
102 refs/remotes/*,commit)
104 refname_type="tracking branch"
105 short_refname=${refname##refs/remotes/}
106 echo >&2 "*** Push-update of tracking branch, $refname"
107 echo >&2 "*** - no email generated."
111 # Anything else (is there anything else?)
112 echo >&2 "*** Unknown type of update to $refname ($rev_type)"
113 echo >&2 "*** - no email generated"
118 # Check if we've got anyone to send to
119 if [ -z "$recipients" ]; then
120 case "$refname_type" in
122 config_name="hooks.post-receive-email.announcelist"
125 config_name="hooks.post-receive-email.mailinglist"
128 echo >&2 "*** $config_name is not set so no email will be sent"
129 echo >&2 "*** for $refname update $oldrev->$newrev"
133 generate_email_header
134 generate_${change_type}_${function}_email
137 generate_email_header()
139 # --- Email (all stdout will be the email)
142 From: ${USER}@payflex.com
144 Subject: ${emailprefix} $short_refname $refname_type ${change_type}d. $describe
145 X-Git-Refname: $refname
146 X-Git-Reftype: $refname_type
147 X-Git-Oldrev: $oldrev
148 X-Git-Newrev: $newrev
150 The $refname_type, $short_refname has been ${change_type}d
155 # --------------- Branches
158 # Called for the creation of a branch
160 generate_create_branch_email()
162 # This is a new branch and so oldrev is not valid
163 echo " at $newrev ($newrev_type)"
167 # This shows all log entries that are not already covered by
168 # another ref - i.e. commits that are now accessible from this
169 # ref that were previously not accessible
170 # (see generate_update_branch_email for the explanation of this
174 echo "$new_commits" | git rev-list --pretty --reverse --stdin
178 echo "Summary of changes:"
180 oldest_new=$(echo "$new_commits" | git rev-list --stdin | tail -n 1)
181 if [ "$oldest_new" != "" ] ; then
182 git diff-tree --stat -p $oldest_new^..$newrev
187 # Called for the change of a pre-existing branch
189 generate_update_branch_email()
192 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
194 # O is $oldrev for $refname
195 # N is $newrev for $refname
196 # X is a revision pointed to by some other ref, for which we may
197 # assume that an email has already been generated.
198 # In this case we want to issue an email containing only revisions
199 # 3, 4, and N. Given (almost) by
201 # git rev-list N ^O --not --all
203 # The reason for the "almost", is that the "--not --all" will take
204 # precedence over the "N", and effectively will translate to
206 # git rev-list N ^O ^X ^N
208 # So, we need to build up the list more carefully. git rev-parse
209 # will generate a list of revs that may be fed into git rev-list.
210 # We can get it to make the "--not --all" part and then filter out
213 # git rev-parse --not --all | grep -v N
215 # Then, using the --stdin switch to git rev-list we have effectively
218 # git rev-list N ^O ^X
220 # This leaves a problem when someone else updates the repository
221 # while this script is running. Their new value of the ref we're
222 # working on would be included in the "--not --all" output; and as
223 # our $newrev would be an ancestor of that commit, it would exclude
224 # all of our commits. What we really want is to exclude the current
225 # value of $refname from the --not list, rather than N itself. So:
227 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
229 # Get's us to something pretty safe (apart from the small time
230 # between refname being read, and git rev-parse running - for that,
234 # Next problem, consider this:
235 # * --- B --- * --- O ($oldrev)
237 # * --- X --- * --- N ($newrev)
239 # That is to say, there is no guarantee that oldrev is a strict
240 # subset of newrev (it would have required a --force, but that's
241 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
242 # Instead we find the common base of the two revs and list from
245 # As above, we need to take into account the presence of X; if
246 # another branch is already in the repository and points at some of
247 # the revisions that we are about to output - we don't want them.
248 # The solution is as before: git rev-parse output filtered.
250 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
252 # Tags pushed into the repository generate nice shortlog emails that
253 # summarise the commits between them and the previous tag. However,
254 # those emails don't include the full commit messages that we output
255 # for a branch update. Therefore we still want to output revisions
256 # that have been output on a tag email.
258 # Luckily, git rev-parse includes just the tool. Instead of using
259 # "--all" we use "--branches"; this has the added benefit that
260 # "remotes/" will be ignored as well.
262 # List all of the revisions that were removed by this update, in a
263 # fast forward update, this list will be empty, because rev-list O
264 # ^N is empty. For a non fast forward, O ^N is the list of removed
268 for rev in $(git rev-list $newrev..$oldrev)
270 revtype=$(git cat-file -t "$rev")
271 echo " discards $rev ($revtype)"
273 if [ -z "$rev" ]; then
277 # List all the revisions from baserev to newrev in a kind of
278 # "table-of-contents"; note this list can include revisions that
279 # have already had notification emails and is present to show the
280 # full detail of the change from rolling back the old revision to
281 # the base revision and then forward to the new revision
282 # Changed: added --first-parent to not go down merge commits
283 for rev in $(git rev-list --first-parent $oldrev..$newrev)
285 revtype=$(git cat-file -t "$rev")
286 echo " via $rev ($revtype)"
289 if [ "$fast_forward" ]; then
290 echo " from $oldrev ($oldrev_type)"
292 # 1. Existing revisions were removed. In this case newrev
293 # is a subset of oldrev - this is the reverse of a
294 # fast-forward, a rewind
295 # 2. New revisions were added on top of an old revision,
296 # this is a rewind and addition.
298 # (1) certainly happened, (2) possibly. When (2) hasn't
299 # happened, we set a flag to indicate that no log printout
304 # Find the common ancestor of the old and new revisions and
305 # compare it with newrev
306 baserev=$(git merge-base $oldrev $newrev)
308 if [ "$baserev" = "$newrev" ]; then
309 echo "This update discarded existing revisions and left the branch pointing at"
310 echo "a previous point in the repository history."
312 echo " * -- * -- N ($newrev)"
314 echo " O -- O -- O ($oldrev)"
316 echo "The removed revisions are not necessarilly gone - if another reference"
317 echo "still refers to them they will stay in the repository."
320 echo "This update added new revisions after undoing existing revisions. That is"
321 echo "to say, the old revision is not a strict subset of the new revision. This"
322 echo "situation occurs when you --force push a change and generate a repository"
323 echo "containing something like this:"
325 echo " * -- * -- B -- O -- O -- O ($oldrev)"
327 echo " N -- N -- N ($newrev)"
329 echo "When this happens we assume that you've already had alert emails for all"
330 echo "of the O revisions, and so we here report only the revisions in the N"
331 echo "branch from the common base, B."
336 if [ -z "$rewind_only" ]; then
337 echo "Those revisions listed above that are new to this repository have"
338 echo "not appeared on any other notification email; so we list those"
339 echo "revisions in full, below."
345 echo "$new_commits" | git rev-list --reverse --pretty --stdin
347 # XXX: Need a way of detecting whether git rev-list actually
348 # outputted anything, so that we can issue a "no new
349 # revisions added by this update" message
353 echo "No new revisions were added by this update."
356 # The diffstat is shown from the old revision to the new revision.
357 # This is to show the truth of what happened in this change.
358 # There's no point showing the stat from the base to the new
359 # revision because the base is effectively a random revision at this
360 # point - the user will be interested in what this revision changed
361 # - including the undoing of previous revisions in the case of
362 # non-fast forward updates.
364 echo "Summary of changes:"
365 git diff-tree --stat -p --find-copies-harder $oldrev..$newrev
369 # Called for the deletion of a branch
371 generate_delete_branch_email()
376 git show -s --pretty=oneline $oldrev
380 # --------------- Annotated tags
383 # Called for the creation of an annotated tag
385 generate_create_atag_email()
387 echo " at $newrev ($newrev_type)"
393 # Called for the update of an annotated tag (this is probably a rare event
394 # and may not even be allowed)
396 generate_update_atag_email()
398 echo " to $newrev ($newrev_type)"
399 echo " from $oldrev (which is now obsolete)"
405 # Called when an annotated tag is created or changed
407 generate_atag_email()
409 # Use git for-each-ref to pull out the individual fields from the
411 eval $(git for-each-ref --shell --format='
412 tagobject=%(*objectname)
413 tagtype=%(*objecttype)
415 tagged=%(taggerdate)' $refname
418 echo " tagging $tagobject ($tagtype)"
422 # If the tagged object is a commit, then we assume this is a
423 # release, and so we calculate which tag this tag is
425 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
427 if [ -n "$prevtag" ]; then
428 echo " replaces $prevtag"
432 echo " length $(git cat-file -s $tagobject) bytes"
435 echo " tagged by $tagger"
441 # Show the content of the tag message; this might contain a change
442 # log or release notes so is worth displaying.
443 git cat-file tag $newrev | sed -e '1,/^$/d'
448 # Only commit tags make sense to have rev-list operations
450 if [ -n "$prevtag" ]; then
451 # Show changes since the previous release
452 git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
454 # No previous tag, show all the changes since time
456 git rev-list --pretty=short $newrev | git shortlog
460 # XXX: Is there anything useful we can do for non-commit
469 # Called for the deletion of an annotated tag
471 generate_delete_atag_email()
476 git show -s --pretty=oneline $oldrev
480 # --------------- General references
483 # Called when any other type of reference is created (most likely a
486 generate_create_ltag_email()
488 echo " at $newrev ($newrev_type)"
494 # Called when any other type of reference is updated (most likely a
497 generate_update_ltag_email()
499 echo " to $newrev ($newrev_type)"
506 # Called for creation or update of any other type of reference
508 generate_ltag_email()
510 # Unannotated tags are more about marking a point than releasing a
511 # version; therefore we don't do the shortlog summary that we do for
512 # annotated tags above - we simply show that the point has been
513 # marked, and print the log message for the marked point for
516 # Note this section also catches any other reference type (although
517 # there aren't any) and deals with them in the same way.
520 if [ "$newrev_type" = "commit" ]; then
522 git show --no-color --root -s --pretty=medium $newrev
525 # What can we do here? The tag marks an object that is not
526 # a commit, so there is no log for us to display. It's
527 # probably not wise to output git cat-file as it could be a
528 # binary blob. We'll just say how big it is
529 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
534 # Called for the deletion of any other type of reference
536 generate_delete_ltag_email()
541 git show -s --pretty=oneline $oldrev
547 if [ -n "$envelopesender" ] ; then
548 /usr/sbin/sendmail -t -f "$envelopesender"
550 # /usr/sbin/sendmail -t
551 /home/BIPFS/shaberman/local/bin/msmtp -t
555 # ---------------------------- main()
558 LOGBEGIN="- Log -----------------------------------------------------------------"
559 LOGEND="-----------------------------------------------------------------------"
562 # Set GIT_DIR either from the working directory, or from the environment
564 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
565 if [ -z "$GIT_DIR" ]; then
566 echo >&2 "fatal: post-receive: GIT_DIR not set"
570 projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
571 # Check if the description is unchanged from it's default, and shorten it to
572 # a more manageable length if it is
573 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
575 projectdesc="UNNAMED PROJECT"
578 recipients=$(git config hooks.post-receive-email.mailinglist)
579 announcerecipients=$(git config hooks.post-receive-email.announcelist)
580 envelopesender=$(git config hooks.post-receive-email.envelopesender)
581 emailprefix="[$projectdesc]"
582 debug=$(git config hooks.post-receive-email.debug)
585 # Allow dual mode: run from the command line just like the update hook, or
586 # if no arguments are given then run as a hook script
587 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
588 # Output to the terminal in command line mode - if someone wanted to
589 # resend an email; they could redirect the output to sendmail
591 PAGER= generate_email $2 $3 $1
593 while read oldrev newrev refname
595 if [ "$debug" == "true" ] ; then
596 generate_email $oldrev $newrev $refname > "${refname//\//.}.out"
598 generate_email $oldrev $newrev $refname | send_mail