Refactoring.
[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 #  - generate_email_footer
62 #
63 generate_email()
64 {
65         # --- Arguments
66         oldrev=$(git rev-parse $1)
67         newrev=$(git rev-parse $2)
68         refname="$3"
69
70         set_change_type
71         set_rev_types
72         set_describe
73
74         # The revision type tells us what type the commit is, combined with
75         # the location of the ref we can decide between
76         #  - working branch
77         #  - tracking branch
78         #  - unannoted tag
79         #  - annotated tag
80         case "$refname","$rev_type" in
81                 refs/tags/*,commit)
82                         # un-annotated tag
83                         refname_type="tag"
84                         function="ltag"
85                         short_refname=${refname##refs/tags/}
86                         ;;
87                 refs/tags/*,tag)
88                         # annotated tag
89                         refname_type="annotated tag"
90                         function="atag"
91                         short_refname=${refname##refs/tags/}
92                         # change recipients
93                         if [ -n "$announcerecipients" ]; then
94                                 recipients="$announcerecipients"
95                         fi
96                         ;;
97                 refs/heads/*,commit)
98                         # branch
99                         refname_type="branch"
100                         function="branch"
101                         short_refname=${refname##refs/heads/}
102                         ;;
103                 refs/remotes/*,commit)
104                         # tracking branch
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."
109                         exit 0
110                         ;;
111                 *)
112                         # Anything else (is there anything else?)
113                         echo >&2 "*** Unknown type of update to $refname ($rev_type)"
114                         echo >&2 "***  - no email generated"
115                         exit 1
116                         ;;
117         esac
118
119         # Check if we've got anyone to send to
120         if [ -z "$recipients" ]; then
121                 case "$refname_type" in
122                         "annotated tag")
123                                 config_name="hooks.post-receive-email.announcelist"
124                                 ;;
125                         *)
126                                 config_name="hooks.post-receive-email.mailinglist"
127                                 ;;
128                 esac
129                 echo >&2 "*** $config_name is not set so no email will be sent"
130                 echo >&2 "*** for $refname update $oldrev->$newrev"
131                 exit 0
132         fi
133
134         generate_email_header
135         generate_${change_type}_${function}_email
136         generate_email_footer
137 }
138
139 generate_email_header()
140 {
141         # --- Email (all stdout will be the email)
142         # Generate header
143         cat <<-EOF
144         From: ${USER}@payflex.com
145         To: $recipients
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
151
152         The $refname_type, $short_refname has been ${change_type}d
153         EOF
154 }
155
156 generate_email_footer()
157 {
158         cat <<-EOF
159
160
161         hooks/post-receive
162         --
163         $projectdesc
164         EOF
165 }
166
167 # --------------- Branches
168
169 #
170 # Called for the creation of a branch
171 #
172 generate_create_branch_email()
173 {
174         # This is a new branch and so oldrev is not valid
175         echo "        at  $newrev ($newrev_type)"
176         echo ""
177
178         echo $LOGBEGIN
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
183         # command)
184
185         set_new_commits
186         echo "$new_commits" | git rev-list --pretty --reverse --stdin
187         echo $LOGEND
188
189         echo ""
190         echo "Summary of changes:"
191
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
195         fi
196 }
197
198 #
199 # Called for the change of a pre-existing branch
200 #
201 generate_update_branch_email()
202 {
203         # Consider this:
204         #   1 --- 2 --- O --- X --- 3 --- 4 --- N
205         #
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
212         #
213         #  git rev-list N ^O --not --all
214         #
215         # The reason for the "almost", is that the "--not --all" will take
216         # precedence over the "N", and effectively will translate to
217         #
218         #  git rev-list N ^O ^X ^N
219         #
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
223         # the "^N" with:
224         #
225         #  git rev-parse --not --all | grep -v N
226         #
227         # Then, using the --stdin switch to git rev-list we have effectively
228         # manufactured
229         #
230         #  git rev-list N ^O ^X
231         #
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:
238         #
239         #  git rev-parse --not --all | grep -v $(git rev-parse $refname)
240         #
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,
243         # I give up)
244         #
245         #
246         # Next problem, consider this:
247         #   * --- B --- * --- O ($oldrev)
248         #          \
249         #           * --- X --- * --- N ($newrev)
250         #
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
255         # there.
256         #
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.
261         #
262         # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
263         #
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.
269         #
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.
273
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
277         # revisions
278         fast_forward=""
279         rev=""
280         for rev in $(git rev-list $newrev..$oldrev)
281         do
282                 revtype=$(git cat-file -t "$rev")
283                 echo "  discards  $rev ($revtype)"
284         done
285         if [ -z "$rev" ]; then
286                 fast_forward=1
287         fi
288
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)
296         do
297                 revtype=$(git cat-file -t "$rev")
298                 echo "       via  $rev ($revtype)"
299         done
300
301         if [ "$fast_forward" ]; then
302                 echo "      from  $oldrev ($oldrev_type)"
303         else
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.
309
310                 # (1) certainly happened, (2) possibly.  When (2) hasn't
311                 # happened, we set a flag to indicate that no log printout
312                 # is required.
313
314                 echo ""
315
316                 # Find the common ancestor of the old and new revisions and
317                 # compare it with newrev
318                 baserev=$(git merge-base $oldrev $newrev)
319                 rewind_only=""
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."
323                         echo ""
324                         echo " * -- * -- N ($newrev)"
325                         echo "            \\"
326                         echo "             O -- O -- O ($oldrev)"
327                         echo ""
328                         echo "The removed revisions are not necessarilly gone - if another reference"
329                         echo "still refers to them they will stay in the repository."
330                         rewind_only=1
331                 else
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:"
336                         echo ""
337                         echo " * -- * -- B -- O -- O -- O ($oldrev)"
338                         echo "            \\"
339                         echo "             N -- N -- N ($newrev)"
340                         echo ""
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."
344                 fi
345         fi
346
347         echo ""
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."
352
353                 echo ""
354                 echo $LOGBEGIN
355
356                 set_new_commits
357                 echo "$new_commits" | git rev-list --reverse --pretty --stdin
358
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
362
363                 echo $LOGEND
364         else
365                 echo "No new revisions were added by this update."
366         fi
367
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.
375         echo ""
376         echo "Summary of changes:"
377         git diff-tree --stat -p --find-copies-harder $oldrev..$newrev
378 }
379
380 #
381 # Called for the deletion of a branch
382 #
383 generate_delete_branch_email()
384 {
385         echo "       was  $oldrev"
386         echo ""
387         echo $LOGEND
388         git show -s --pretty=oneline $oldrev
389         echo $LOGEND
390 }
391
392 # --------------- Annotated tags
393
394 #
395 # Called for the creation of an annotated tag
396 #
397 generate_create_atag_email()
398 {
399         echo "        at  $newrev ($newrev_type)"
400
401         generate_atag_email
402 }
403
404 #
405 # Called for the update of an annotated tag (this is probably a rare event
406 # and may not even be allowed)
407 #
408 generate_update_atag_email()
409 {
410         echo "        to  $newrev ($newrev_type)"
411         echo "      from  $oldrev (which is now obsolete)"
412
413         generate_atag_email
414 }
415
416 #
417 # Called when an annotated tag is created or changed
418 #
419 generate_atag_email()
420 {
421         # Use git for-each-ref to pull out the individual fields from the
422         # tag
423         eval $(git for-each-ref --shell --format='
424         tagobject=%(*objectname)
425         tagtype=%(*objecttype)
426         tagger=%(taggername)
427         tagged=%(taggerdate)' $refname
428         )
429
430         echo "   tagging  $tagobject ($tagtype)"
431         case "$tagtype" in
432         commit)
433
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
436                 # replacing
437                 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
438
439                 if [ -n "$prevtag" ]; then
440                         echo "  replaces  $prevtag"
441                 fi
442                 ;;
443         *)
444                 echo "    length  $(git cat-file -s $tagobject) bytes"
445                 ;;
446         esac
447         echo " tagged by  $tagger"
448         echo "        on  $tagged"
449
450         echo ""
451         echo $LOGBEGIN
452
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'
456
457         echo ""
458         case "$tagtype" in
459         commit)
460                 # Only commit tags make sense to have rev-list operations
461                 # performed on them
462                 if [ -n "$prevtag" ]; then
463                         # Show changes since the previous release
464                         git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
465                 else
466                         # No previous tag, show all the changes since time
467                         # began
468                         git rev-list --pretty=short $newrev | git shortlog
469                 fi
470                 ;;
471         *)
472                 # XXX: Is there anything useful we can do for non-commit
473                 # objects?
474                 ;;
475         esac
476
477         echo $LOGEND
478 }
479
480 #
481 # Called for the deletion of an annotated tag
482 #
483 generate_delete_atag_email()
484 {
485         echo "       was  $oldrev"
486         echo ""
487         echo $LOGEND
488         git show -s --pretty=oneline $oldrev
489         echo $LOGEND
490 }
491
492 # --------------- General references
493
494 #
495 # Called when any other type of reference is created (most likely a
496 # non-annotated tag)
497 #
498 generate_create_ltag_email()
499 {
500         echo "        at  $newrev ($newrev_type)"
501
502         generate_ltag_email
503 }
504
505 #
506 # Called when any other type of reference is updated (most likely a
507 # non-annotated tag)
508 #
509 generate_update_ltag_email()
510 {
511         echo "        to  $newrev ($newrev_type)"
512         echo "      from  $oldrev"
513
514         generate_ltag_email
515 }
516
517 #
518 # Called for creation or update of any other type of reference
519 #
520 generate_ltag_email()
521 {
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
526         # reference purposes
527         #
528         # Note this section also catches any other reference type (although
529         # there aren't any) and deals with them in the same way.
530
531         echo ""
532         if [ "$newrev_type" = "commit" ]; then
533                 echo $LOGBEGIN
534                 git show --no-color --root -s --pretty=medium $newrev
535                 echo $LOGEND
536         else
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."
542         fi
543 }
544
545 #
546 # Called for the deletion of any other type of reference
547 #
548 generate_delete_ltag_email()
549 {
550         echo "       was  $oldrev"
551         echo ""
552         echo $LOGEND
553         git show -s --pretty=oneline $oldrev
554         echo $LOGEND
555 }
556
557 send_mail()
558 {
559         if [ -n "$envelopesender" ] ; then
560                 /usr/sbin/sendmail -t -f "$envelopesender"
561         else
562                 # /usr/sbin/sendmail -t
563                 /home/BIPFS/shaberman/local/bin/msmtp -t
564         fi
565 }
566
567 # ---------------------------- main()
568
569 # --- Constants
570 LOGBEGIN="- Log -----------------------------------------------------------------"
571 LOGEND="-----------------------------------------------------------------------"
572
573 # --- Config
574 # Set GIT_DIR either from the working directory, or from the environment
575 # variable.
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"
579         exit 1
580 fi
581
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
586 then
587         projectdesc="UNNAMED PROJECT"
588 fi
589
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)
595
596 # --- Main loop
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
602         # themselves
603         PAGER= generate_email $2 $3 $1
604 else
605         while read oldrev newrev refname
606         do
607                 if [ "$debug" == "true" ] ; then
608                         generate_email $oldrev $newrev $refname > "${refname//\//.}.out"
609                 else
610                         generate_email $oldrev $newrev $refname | send_mail
611                 fi
612         done
613 fi