hooks: don't include merges in the tag commit summary
[dragonfly.git] / 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.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.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.envelopesender
36 #   If set then the -f option is passed to sendmail to allow the envelope
37 #   sender address to be set
38 # hooks.emailprefix
39 #   All emails have their subjects prefixed with this prefix, or "[SCM]"
40 #   if emailprefix is unset, to aid filtering
41 #
42 # Notes
43 # -----
44 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
45 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
46 # give information for debugging.
47 #
48
49 # ---------------------------- Functions
50
51 #
52 # Top level email generation function.  This decides what type of update
53 # this is and calls the appropriate body-generation routine after outputting
54 # the common header
55 #
56 # Note this function doesn't actually generate any email output, that is
57 # taken care of by the functions it calls:
58 #  - generate_email_header
59 #  - generate_create_XXXX_email
60 #  - generate_update_XXXX_email
61 #  - generate_delete_XXXX_email
62 #  - generate_email_footer
63 #
64 generate_email()
65 {
66         # --- Arguments
67         oldrev=$(git rev-parse $1)
68         newrev=$(git rev-parse $2)
69         refname="$3"
70
71         # --- Interpret
72         # 0000->1234 (create)
73         # 1234->2345 (update)
74         # 2345->0000 (delete)
75         if expr "$oldrev" : '0*$' >/dev/null
76         then
77                 change_type="create"
78         else
79                 if expr "$newrev" : '0*$' >/dev/null
80                 then
81                         change_type="delete"
82                 else
83                         change_type="update"
84                 fi
85         fi
86
87         # --- Get the revision types
88         newrev_type=$(git cat-file -t $newrev 2> /dev/null)
89         oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
90         case "$change_type" in
91         create|update)
92                 rev="$newrev"
93                 rev_type="$newrev_type"
94                 ;;
95         delete)
96                 rev="$oldrev"
97                 rev_type="$oldrev_type"
98                 ;;
99         esac
100
101         # The revision type tells us what type the commit is, combined with
102         # the location of the ref we can decide between
103         #  - working branch
104         #  - tracking branch
105         #  - unannoted tag
106         #  - annotated tag
107         case "$refname","$rev_type" in
108                 refs/tags/*,commit)
109                         # un-annotated tag
110                         refname_type="tag"
111                         short_refname=${refname##refs/tags/}
112                         ;;
113                 refs/tags/*,tag)
114                         # annotated tag
115                         refname_type="annotated tag"
116                         short_refname=${refname##refs/tags/}
117                         # change recipients
118                         if [ -n "$announcerecipients" ]; then
119                                 recipients="$announcerecipients"
120                         fi
121                         ;;
122                 refs/heads/*,commit)
123                         # branch
124                         refname_type="branch"
125                         short_refname=${refname##refs/heads/}
126                         ;;
127                 refs/remotes/*,commit)
128                         # tracking branch
129                         refname_type="tracking branch"
130                         short_refname=${refname##refs/remotes/}
131                         echo >&2 "*** Push-update of tracking branch, $refname"
132                         echo >&2 "***  - no email generated."
133                         exit 0
134                         ;;
135                 *)
136                         # Anything else (is there anything else?)
137                         echo >&2 "*** Unknown type of update to $refname ($rev_type)"
138                         echo >&2 "***  - no email generated"
139                         exit 1
140                         ;;
141         esac
142
143         # Check if we've got anyone to send to
144         if [ -z "$recipients" ]; then
145                 case "$refname_type" in
146                         "annotated tag")
147                                 config_name="hooks.announcelist"
148                                 ;;
149                         *)
150                                 config_name="hooks.mailinglist"
151                                 ;;
152                 esac
153                 echo >&2 "*** $config_name is not set so no email will be sent"
154                 echo >&2 "*** for $refname update $oldrev->$newrev"
155                 exit 0
156         fi
157
158         # The email subject will contain the best description of the ref
159         # that we can build from the parameters
160         set_describe $rev
161
162         # Call the correct body generation function
163         fn_name=general
164         case "$refname_type" in
165         "tracking branch"|branch)
166                 fn_name=branch
167                 ;;
168         "annotated tag")
169                 fn_name=atag
170                 ;;
171         esac
172
173         case "$fn_name" in
174         branch)
175                 revs=$(list_${change_type}_branch_revs 2>/dev/null)
176
177                 if [ "$change_type" != "update" ]
178                 then
179                         set_update_branch_subject
180                         {
181                         generate_email_header
182                         generate_${change_type}_branch_email
183                         generate_email_footer
184                         } | $send_mail
185                 fi
186
187                 for rev in $revs
188                 do
189                         change_type=update
190                         oldrev=
191                         newrev=$rev
192
193                         # set_update_branch_subject will return false if the
194                         # commit is boring (no change).  Then we don't even
195                         # send a mail.
196                         set_update_branch_subject || continue
197                         {
198                         generate_email_header
199                         print_change_info $newrev
200                         summarize_branch_revs
201                         generate_email_footer
202                         } | $send_mail
203                 done
204                 ;;
205
206         *)
207                 {
208                 generate_email_header
209                 generate_${change_type}_${fn_name}_email
210                 generate_email_footer
211                 } | $send_mail
212                 ;;
213         esac
214 }
215
216 set_describe()
217 {
218         describe=$(git describe --abbrev=4 $1 2>/dev/null || git rev-parse --short $1)
219         describe=$(echo "$describe" | sed -e 's/-/./g;s/^v//;')
220 }
221
222 generate_email_header()
223 {
224         # --- Email (all stdout will be the email)
225         # Generate header
226         print_change_type=" ${change_type}d"
227         case "$change_type" in
228         "update")
229                 print_change_type=""
230                 ;;
231         esac
232
233         print_refname="$refname_type "
234         case "$refname_type" in
235         "branch")
236                 print_refname=""
237                 ;;
238         esac
239
240         cat <<-EOF
241         To: $recipients
242         Subject: ${emailprefix}$describe $print_refname$short_refname${print_change_type}$detail
243         X-Git-Refname: $refname
244         X-Git-Reftype: $refname_type
245         X-Git-Newrev: $newrev
246
247         EOF
248 }
249
250 generate_email_footer()
251 {
252         SPACE=" "
253         cat <<-EOF
254
255         --${SPACE}
256         $projectdesc
257         EOF
258 }
259
260 # --------------- Branches
261
262 #
263 # Called for the creation of a branch
264 #
265 generate_create_branch_email()
266 {
267         # This is a new branch and so oldrev is not valid
268         echo "        at  $newrev ($newrev_type)"
269         echo ""
270 }
271
272 generic_list_branch_revs()
273 {
274         range=$1
275
276         # We want to list all revs that are reachable now, but
277         # were not before.
278         # All revs that were reachable before are git rev-parse --branches.
279         # However, this includes $refname.  The naive | grep -v $branchtip
280         # will not work, because another branch might already have been on
281         # $branchtip.  In this case we shouldn't list any rev.
282         # As git rev-parse --branches might list a given rev multiple times
283         # if there are multiple branches at this rev, we simply drop this
284         # $branchtip rev once, and pass all subsequent ones through.
285
286         branchtip=$(git rev-parse $refname)
287
288         git rev-parse --not --branches | sed -e "1,/$branchtip/{/$branchtip/d;}" |
289         git rev-list --reverse --stdin $range
290 }
291
292 list_create_branch_revs()
293 {
294         generic_list_branch_revs $newrev
295 }
296
297 #
298 # Called for the change of a pre-existing branch
299 #
300 generate_update_branch_email()
301 {
302         # XXX only valid for fast forwards
303
304         # List all the revisions from baserev to newrev in a kind of
305         # "table-of-contents"; note this list can include revisions that
306         # have already had notification emails and is present to show the
307         # full detail of the change from rolling back the old revision to
308         # the base revision and then forward to the new revision
309         for rev in $(git rev-list $oldrev..$newrev)
310         do
311                 revtype=$(git cat-file -t "$rev")
312                 echo "       via  $rev ($revtype)"
313         done
314
315         echo "      from  $oldrev ($oldrev_type)"
316
317         echo ""
318         echo $LOGBEGIN
319 }
320
321 skip_diff_tree_parent()
322 {
323         if [ -n "$oldrev" ]
324         then
325                 cat
326         else
327                 tail +2
328         fi
329 }
330
331 set_update_branch_subject()
332 {
333         set_describe $newrev
334
335         detail=$(git diff-tree -r -c --name-only $oldrev${oldrev:+..}$newrev 2>/dev/null |
336                         skip_diff_tree_parent |
337                         sed -E -e 's#/([^/]*)$# \1#' |
338                         awk 'pos > 74 {
339                                 printf "\n";
340                                 pos = 0;
341                         }
342                         $1 != lastdir {
343                                 printf " %s", $1;
344                                 pos += length($1) + 1;
345                                 lastdir = $1;
346                         }
347                         $2 != "" {
348                                 printf " %s", $2;
349                                 pos += length($2) + 1;
350                         }')
351         [ -n "$(git diff-tree -r --name-only --cc $oldrev${oldrev:+..}$newrev 2>/dev/null |
352                 skip_diff_tree_parent)" ]
353 }
354
355 list_update_branch_revs()
356 {
357         generic_list_branch_revs $oldrev..$newrev
358 }
359
360 summarize_branch_revs()
361 {
362         echo "Summary of changes:"
363         git diff-tree --stat --summary --find-copies-harder $oldrev${oldrev:+..}$newrev | skip_diff_tree_parent
364
365         if [ -n "$gitweburl" ]
366         then
367                 echo ""
368                 echo "$gitweburl/$reponame/commitdiff/$oldrev${oldrev:+..}$newrev"
369         fi
370         echo $LOGEND
371 }
372
373 print_change_info()
374 {
375         echo $LOGBEGIN
376         git rev-list -n 1 --pretty $1
377 }
378
379 #
380 # Called for the deletion of a branch
381 #
382 generate_delete_branch_email()
383 {
384         echo "       was  $oldrev"
385         echo ""
386         echo $LOGEND
387         git show -s --pretty=oneline $oldrev
388         echo $LOGEND
389 }
390
391 # --------------- Annotated tags
392
393 #
394 # Called for the creation of an annotated tag
395 #
396 generate_create_atag_email()
397 {
398         echo "        at  $newrev ($newrev_type)"
399
400         generate_atag_email
401 }
402
403 #
404 # Called for the update of an annotated tag (this is probably a rare event
405 # and may not even be allowed)
406 #
407 generate_update_atag_email()
408 {
409         echo "        to  $newrev ($newrev_type)"
410         echo "      from  $oldrev (which is now obsolete)"
411
412         generate_atag_email
413 }
414
415 #
416 # Called when an annotated tag is created or changed
417 #
418 generate_atag_email()
419 {
420         # Use git for-each-ref to pull out the individual fields from the
421         # tag
422         eval $(git for-each-ref --shell --format='
423         tagobject=%(*objectname)
424         tagtype=%(*objecttype)
425         tagger=%(taggername)
426         tagged=%(taggerdate)' $refname
427         )
428
429         echo "   tagging  $tagobject ($tagtype)"
430         case "$tagtype" in
431         commit)
432
433                 # If the tagged object is a commit, then we assume this is a
434                 # release, and so we calculate which tag this tag is
435                 # replacing
436                 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
437
438                 if [ -n "$prevtag" ]; then
439                         echo "  replaces  $prevtag"
440                 fi
441                 ;;
442         *)
443                 echo "    length  $(git cat-file -s $tagobject) bytes"
444                 ;;
445         esac
446         echo " tagged by  $tagger"
447         echo "        on  $tagged"
448
449         echo ""
450         echo $LOGBEGIN
451
452         # Show the content of the tag message; this might contain a change
453         # log or release notes so is worth displaying.
454         git cat-file tag $newrev | sed -e '1,/^$/d'
455
456         echo ""
457         case "$tagtype" in
458         commit)
459                 # Only commit tags make sense to have rev-list operations
460                 # performed on them
461                 if [ -n "$prevtag" ]; then
462                         # Show changes since the previous release
463                         git rev-list --no-merges --pretty=short "$prevtag..$newrev" | git shortlog
464                 else
465                         # No previous tag, show all the changes since time
466                         # began, but only summarize due to the possibly large size.
467                         git rev-list --no-merges --pretty=short $newrev | git shortlog -s
468                 fi
469                 ;;
470         *)
471                 # XXX: Is there anything useful we can do for non-commit
472                 # objects?
473                 ;;
474         esac
475
476         echo $LOGEND
477 }
478
479 #
480 # Called for the deletion of an annotated tag
481 #
482 generate_delete_atag_email()
483 {
484         echo "       was  $oldrev"
485         echo ""
486         echo $LOGEND
487         git show -s --pretty=oneline $oldrev
488         echo $LOGEND
489 }
490
491 # --------------- General references
492
493 #
494 # Called when any other type of reference is created (most likely a
495 # non-annotated tag)
496 #
497 generate_create_general_email()
498 {
499         echo "        at  $newrev ($newrev_type)"
500
501         generate_general_email
502 }
503
504 #
505 # Called when any other type of reference is updated (most likely a
506 # non-annotated tag)
507 #
508 generate_update_general_email()
509 {
510         echo "        to  $newrev ($newrev_type)"
511         echo "      from  $oldrev"
512
513         generate_general_email
514 }
515
516 #
517 # Called for creation or update of any other type of reference
518 #
519 generate_general_email()
520 {
521         # Unannotated tags are more about marking a point than releasing a
522         # version; therefore we don't do the shortlog summary that we do for
523         # annotated tags above - we simply show that the point has been
524         # marked, and print the log message for the marked point for
525         # reference purposes
526         #
527         # Note this section also catches any other reference type (although
528         # there aren't any) and deals with them in the same way.
529
530         echo ""
531         if [ "$newrev_type" = "commit" ]; then
532                 echo $LOGBEGIN
533                 git show --no-color --root -s --pretty=medium $newrev
534                 echo $LOGEND
535         else
536                 # What can we do here?  The tag marks an object that is not
537                 # a commit, so there is no log for us to display.  It's
538                 # probably not wise to output git cat-file as it could be a
539                 # binary blob.  We'll just say how big it is
540                 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
541         fi
542 }
543
544 #
545 # Called for the deletion of any other type of reference
546 #
547 generate_delete_general_email()
548 {
549         echo "       was  $oldrev"
550         echo ""
551         echo $LOGEND
552         git show -s --pretty=oneline $oldrev
553         echo $LOGEND
554 }
555
556 send_mail()
557 {
558         if [ -n "$envelopesender" ]; then
559                 /usr/sbin/sendmail -t -f "$envelopesender"
560         else
561                 /usr/sbin/sendmail -t
562         fi
563 }
564
565 # ---------------------------- main()
566
567 # --- Constants
568 LOGBEGIN=""
569 LOGEND=""
570
571 # --- Config
572 # Set GIT_DIR either from the working directory, or from the environment
573 # variable.
574 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
575 if [ -z "$GIT_DIR" ]; then
576         echo >&2 "fatal: post-receive: GIT_DIR not set"
577         exit 1
578 fi
579
580 projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
581 # Check if the description is unchanged from it's default, and shorten it to
582 # a more manageable length if it is
583 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
584 then
585         projectdesc="UNNAMED PROJECT"
586 fi
587
588 recipients=$(git config hooks.mailinglist)
589 announcerecipients=$(git config hooks.announcelist)
590 envelopesender=$(git config hooks.envelopesender)
591 emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')
592 gitweburl=$(git config hooks.gitweburl)
593 reponame=$(git config hooks.reponame || basename $(realpath $GIT_DIR))
594
595 # --- Main loop
596 # Allow dual mode: run from the command line just like the update hook, or
597 # if no arguments are given then run as a hook script
598 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
599         # Output to the terminal in command line mode - if someone wanted to
600         # resend an email; they could redirect the output to sendmail
601         # themselves
602         send_mail=cat
603         generate_email $2 $3 $1
604 else
605         while read oldrev newrev refname
606         do
607                 send_mail=send_mail
608                 generate_email $oldrev $newrev $refname
609         done
610 fi