df8bdfe39b63ae3f7c66c432ad1195d6d9366a96
[git-central.git] / server / post-receive-email
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Andy Parkins
4 #
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.
9 #
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:
15 #
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
19 #
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.
25 #
26 # Config
27 # ------
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
38 #
39 # Notes
40 # -----
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.
44 #
45
46 # ---------------------------- Functions
47
48 . $(dirname $0)/functions
49
50 #
51 # Top level email generation function.  This decides what type of update
52 # this is and calls the appropriate body-generation routine after outputting
53 # the common header
54 #
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 #
62 generate_email()
63 {
64         # --- Arguments
65         oldrev=$(git rev-parse $1)
66         newrev=$(git rev-parse $2)
67         refname="$3"
68
69         set_change_type
70         set_rev_types
71         set_describe
72
73         # The revision type tells us what type the commit is, combined with
74         # the location of the ref we can decide between
75         #  - working branch
76         #  - tracking branch
77         #  - unannoted tag
78         #  - annotated tag
79         case "$refname","$rev_type" in
80                 refs/tags/*,commit)
81                         # un-annotated tag
82                         refname_type="tag"
83                         function="ltag"
84                         short_refname=${refname##refs/tags/}
85                         ;;
86                 refs/tags/*,tag)
87                         # annotated tag
88                         refname_type="annotated tag"
89                         function="atag"
90                         short_refname=${refname##refs/tags/}
91                         # change recipients
92                         if [ -n "$announcerecipients" ]; then
93                                 recipients="$announcerecipients"
94                         fi
95                         ;;
96                 refs/heads/*,commit)
97                         # branch
98                         refname_type="branch"
99                         function="branch"
100                         short_refname=${refname##refs/heads/}
101                         ;;
102                 refs/remotes/*,commit)
103                         # tracking branch
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."
108                         exit 0
109                         ;;
110                 *)
111                         # Anything else (is there anything else?)
112                         echo >&2 "*** Unknown type of update to $refname ($rev_type)"
113                         echo >&2 "***  - no email generated"
114                         exit 1
115                         ;;
116         esac
117
118         # Check if we've got anyone to send to
119         if [ -z "$recipients" ]; then
120                 case "$refname_type" in
121                         "annotated tag")
122                                 config_name="hooks.post-receive-email.announcelist"
123                                 ;;
124                         *)
125                                 config_name="hooks.post-receive-email.mailinglist"
126                                 ;;
127                 esac
128                 echo >&2 "*** $config_name is not set so no email will be sent"
129                 echo >&2 "*** for $refname update $oldrev->$newrev"
130                 exit 0
131         fi
132
133         generate_email_header
134         generate_${change_type}_${function}_email
135 }
136
137 generate_email_header()
138 {
139         # --- Email (all stdout will be the email)
140         # Generate header
141         cat <<-EOF
142         From: ${USER}@payflex.com
143         To: $recipients
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
149
150         The $refname_type, $short_refname has been ${change_type}d
151         EOF
152 }
153
154
155 # --------------- Branches
156
157 #
158 # Called for the creation of a branch
159 #
160 generate_create_branch_email()
161 {
162         # This is a new branch and so oldrev is not valid
163         echo "        at  $newrev ($newrev_type)"
164         echo ""
165
166         echo $LOGBEGIN
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
171         # command)
172
173         set_new_commits
174         echo "$new_commits" | git rev-list --pretty --reverse --stdin
175         echo $LOGEND
176
177         echo ""
178         echo "Summary of changes:"
179
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
183         fi
184 }
185
186 #
187 # Called for the change of a pre-existing branch
188 #
189 generate_update_branch_email()
190 {
191         # Consider this:
192         #   1 --- 2 --- O --- X --- 3 --- 4 --- N
193         #
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
200         #
201         #  git rev-list N ^O --not --all
202         #
203         # The reason for the "almost", is that the "--not --all" will take
204         # precedence over the "N", and effectively will translate to
205         #
206         #  git rev-list N ^O ^X ^N
207         #
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
211         # the "^N" with:
212         #
213         #  git rev-parse --not --all | grep -v N
214         #
215         # Then, using the --stdin switch to git rev-list we have effectively
216         # manufactured
217         #
218         #  git rev-list N ^O ^X
219         #
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:
226         #
227         #  git rev-parse --not --all | grep -v $(git rev-parse $refname)
228         #
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,
231         # I give up)
232         #
233         #
234         # Next problem, consider this:
235         #   * --- B --- * --- O ($oldrev)
236         #          \
237         #           * --- X --- * --- N ($newrev)
238         #
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
243         # there.
244         #
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.
249         #
250         # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
251         #
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.
257         #
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.
261
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
265         # revisions
266         fast_forward=""
267         rev=""
268         for rev in $(git rev-list $newrev..$oldrev)
269         do
270                 revtype=$(git cat-file -t "$rev")
271                 echo "  discards  $rev ($revtype)"
272         done
273         if [ -z "$rev" ]; then
274                 fast_forward=1
275         fi
276
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)
284         do
285                 revtype=$(git cat-file -t "$rev")
286                 echo "       via  $rev ($revtype)"
287         done
288
289         if [ "$fast_forward" ]; then
290                 echo "      from  $oldrev ($oldrev_type)"
291         else
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.
297
298                 # (1) certainly happened, (2) possibly.  When (2) hasn't
299                 # happened, we set a flag to indicate that no log printout
300                 # is required.
301
302                 echo ""
303
304                 # Find the common ancestor of the old and new revisions and
305                 # compare it with newrev
306                 baserev=$(git merge-base $oldrev $newrev)
307                 rewind_only=""
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."
311                         echo ""
312                         echo " * -- * -- N ($newrev)"
313                         echo "            \\"
314                         echo "             O -- O -- O ($oldrev)"
315                         echo ""
316                         echo "The removed revisions are not necessarilly gone - if another reference"
317                         echo "still refers to them they will stay in the repository."
318                         rewind_only=1
319                 else
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:"
324                         echo ""
325                         echo " * -- * -- B -- O -- O -- O ($oldrev)"
326                         echo "            \\"
327                         echo "             N -- N -- N ($newrev)"
328                         echo ""
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."
332                 fi
333         fi
334
335         echo ""
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."
340
341                 echo ""
342                 echo $LOGBEGIN
343
344                 set_new_commits
345                 echo "$new_commits" | git rev-list --reverse --pretty --stdin
346
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
350
351                 echo $LOGEND
352         else
353                 echo "No new revisions were added by this update."
354         fi
355
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.
363         echo ""
364         echo "Summary of changes:"
365         git diff-tree --stat -p --find-copies-harder $oldrev..$newrev
366 }
367
368 #
369 # Called for the deletion of a branch
370 #
371 generate_delete_branch_email()
372 {
373         echo "       was  $oldrev"
374         echo ""
375         echo $LOGEND
376         git show -s --pretty=oneline $oldrev
377         echo $LOGEND
378 }
379
380 # --------------- Annotated tags
381
382 #
383 # Called for the creation of an annotated tag
384 #
385 generate_create_atag_email()
386 {
387         echo "        at  $newrev ($newrev_type)"
388
389         generate_atag_email
390 }
391
392 #
393 # Called for the update of an annotated tag (this is probably a rare event
394 # and may not even be allowed)
395 #
396 generate_update_atag_email()
397 {
398         echo "        to  $newrev ($newrev_type)"
399         echo "      from  $oldrev (which is now obsolete)"
400
401         generate_atag_email
402 }
403
404 #
405 # Called when an annotated tag is created or changed
406 #
407 generate_atag_email()
408 {
409         # Use git for-each-ref to pull out the individual fields from the
410         # tag
411         eval $(git for-each-ref --shell --format='
412         tagobject=%(*objectname)
413         tagtype=%(*objecttype)
414         tagger=%(taggername)
415         tagged=%(taggerdate)' $refname
416         )
417
418         echo "   tagging  $tagobject ($tagtype)"
419         case "$tagtype" in
420         commit)
421
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
424                 # replacing
425                 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
426
427                 if [ -n "$prevtag" ]; then
428                         echo "  replaces  $prevtag"
429                 fi
430                 ;;
431         *)
432                 echo "    length  $(git cat-file -s $tagobject) bytes"
433                 ;;
434         esac
435         echo " tagged by  $tagger"
436         echo "        on  $tagged"
437
438         echo ""
439         echo $LOGBEGIN
440
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'
444
445         echo ""
446         case "$tagtype" in
447         commit)
448                 # Only commit tags make sense to have rev-list operations
449                 # performed on them
450                 if [ -n "$prevtag" ]; then
451                         # Show changes since the previous release
452                         git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
453                 else
454                         # No previous tag, show all the changes since time
455                         # began
456                         git rev-list --pretty=short $newrev | git shortlog
457                 fi
458                 ;;
459         *)
460                 # XXX: Is there anything useful we can do for non-commit
461                 # objects?
462                 ;;
463         esac
464
465         echo $LOGEND
466 }
467
468 #
469 # Called for the deletion of an annotated tag
470 #
471 generate_delete_atag_email()
472 {
473         echo "       was  $oldrev"
474         echo ""
475         echo $LOGEND
476         git show -s --pretty=oneline $oldrev
477         echo $LOGEND
478 }
479
480 # --------------- General references
481
482 #
483 # Called when any other type of reference is created (most likely a
484 # non-annotated tag)
485 #
486 generate_create_ltag_email()
487 {
488         echo "        at  $newrev ($newrev_type)"
489
490         generate_ltag_email
491 }
492
493 #
494 # Called when any other type of reference is updated (most likely a
495 # non-annotated tag)
496 #
497 generate_update_ltag_email()
498 {
499         echo "        to  $newrev ($newrev_type)"
500         echo "      from  $oldrev"
501
502         generate_ltag_email
503 }
504
505 #
506 # Called for creation or update of any other type of reference
507 #
508 generate_ltag_email()
509 {
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
514         # reference purposes
515         #
516         # Note this section also catches any other reference type (although
517         # there aren't any) and deals with them in the same way.
518
519         echo ""
520         if [ "$newrev_type" = "commit" ]; then
521                 echo $LOGBEGIN
522                 git show --no-color --root -s --pretty=medium $newrev
523                 echo $LOGEND
524         else
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."
530         fi
531 }
532
533 #
534 # Called for the deletion of any other type of reference
535 #
536 generate_delete_ltag_email()
537 {
538         echo "       was  $oldrev"
539         echo ""
540         echo $LOGEND
541         git show -s --pretty=oneline $oldrev
542         echo $LOGEND
543 }
544
545 send_mail()
546 {
547         if [ -n "$envelopesender" ] ; then
548                 /usr/sbin/sendmail -t -f "$envelopesender"
549         else
550                 # /usr/sbin/sendmail -t
551                 /home/BIPFS/shaberman/local/bin/msmtp -t
552         fi
553 }
554
555 # ---------------------------- main()
556
557 # --- Constants
558 LOGBEGIN="- Log -----------------------------------------------------------------"
559 LOGEND="-----------------------------------------------------------------------"
560
561 # --- Config
562 # Set GIT_DIR either from the working directory, or from the environment
563 # variable.
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"
567         exit 1
568 fi
569
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
574 then
575         projectdesc="UNNAMED PROJECT"
576 fi
577
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)
583
584 # --- Main loop
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
590         # themselves
591         PAGER= generate_email $2 $3 $1
592 else
593         while read oldrev newrev refname
594         do
595                 if [ "$debug" == "true" ] ; then
596                         generate_email $oldrev $newrev $refname > "${refname//\//.}.out"
597                 else
598                         generate_email $oldrev $newrev $refname | send_mail
599                 fi
600         done
601 fi