Change the subject to be more terse
[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                 split=""
176                 revs=$(list_${change_type}_branch_revs 2>/dev/null)
177                 if [ "$(echo "$revs" | wc -l)" -gt 1 ]
178                 then
179                         split=1
180                 fi
181
182                 if [ "$change_type" != "update" ]
183                 then
184                         set_update_branch_subject
185                         {
186                         generate_email_header
187                         generate_${change_type}_branch_email
188                         summarize_branch_revs
189                         generate_email_footer
190                         } | $send_mail
191                 fi
192
193                 for rev in $revs
194                 do
195                         
196                         oldrev=
197                         newrev=$rev
198
199                         # set_update_branch_subject will return false if the
200                         # commit is boring (no change).  Then we don't even
201                         # send a mail.
202                         set_update_branch_subject || continue
203                         {
204                         generate_email_header
205                         print_change_info $newrev
206                         summarize_branch_revs
207                         generate_email_footer
208                         } | $send_mail
209                 done
210                 ;;
211
212         *)
213                 {
214                 generate_email_header
215                 generate_${change_type}_${fn_name}_email
216                 generate_email_footer
217                 } | $send_mail
218                 ;;
219         esac
220 }
221
222 set_describe()
223 {
224         describe=$(git describe --abbrev=4 $1 2>/dev/null || git rev-parse --short $1)
225         describe=$(echo "$describe" | sed -e 's/-/./g;s/^v//;')
226 }
227
228 generate_email_header()
229 {
230         # --- Email (all stdout will be the email)
231         # Generate header
232         print_change_type=" ${change_type}d"
233         case "$change_type" in
234         "update")
235                 print_change_type=""
236                 ;;
237         esac
238
239         print_refname="$refname_type "
240         case "$refname_type" in
241         "branch")
242                 print_refname=""
243                 ;;
244         esac
245
246         cat <<-EOF
247         To: $recipients
248         Subject: ${emailprefix}$describe $print_refname$short_refname${print_change_type}$detail
249         X-Git-Refname: $refname
250         X-Git-Reftype: $refname_type
251         X-Git-Newrev: $newrev
252
253         EOF
254 }
255
256 generate_email_footer()
257 {
258         SPACE=" "
259         cat <<-EOF
260
261         --${SPACE}
262         $projectdesc
263         EOF
264 }
265
266 # --------------- Branches
267
268 #
269 # Called for the creation of a branch
270 #
271 generate_create_branch_email()
272 {
273         # This is a new branch and so oldrev is not valid
274         echo "        at  $newrev ($newrev_type)"
275 }
276
277 list_create_branch_revs()
278 {
279         git rev-parse --not --branches | grep -v $(git rev-parse $refname) |
280         git rev-list --reverse --stdin $newrev
281 }
282
283 #
284 # Called for the change of a pre-existing branch
285 #
286 generate_update_branch_email()
287 {
288         # XXX only valid for fast forwards
289
290         # List all the revisions from baserev to newrev in a kind of
291         # "table-of-contents"; note this list can include revisions that
292         # have already had notification emails and is present to show the
293         # full detail of the change from rolling back the old revision to
294         # the base revision and then forward to the new revision
295         for rev in $(git rev-list $oldrev..$newrev)
296         do
297                 revtype=$(git cat-file -t "$rev")
298                 echo "       via  $rev ($revtype)"
299         done
300
301         echo "      from  $oldrev ($oldrev_type)"
302
303         echo ""
304 }
305
306 skip_diff_tree_parent()
307 {
308         if [ -n "$oldrev" ]
309         then
310                 cat
311         else
312                 tail +2
313         fi
314 }
315
316 set_update_branch_subject()
317 {
318         set_describe $newrev
319
320         detail=$(git diff-tree -r -c --name-only $oldrev${oldrev:+..}$newrev |
321                         skip_diff_tree_parent |
322                         sed -E -e 's#/([^/]*)$# \1#' |
323                         awk 'pos > 74 {
324                                 printf "\n";
325                                 pos = 0;
326                         }
327                         $1 != lastdir {
328                                 printf " %s", $1;
329                                 pos += length($1) + 1;
330                                 lastdir = $1;
331                         }
332                         $2 != "" {
333                                 printf " %s", $2;
334                                 pos += length($2) + 1;
335                         }')
336         [ -n "$(git diff-tree -r --name-only --cc $oldrev${oldrev:+..}$newrev |
337                 skip_diff_tree_parent)" ]
338 }
339
340 list_update_branch_revs()
341 {
342         git rev-parse --not --branches | grep -v $(git rev-parse $refname) |
343         git rev-list --reverse --stdin $oldrev..$newrev
344 }
345
346 summarize_branch_revs()
347 {
348         echo "Summary of changes:"
349         git diff-tree --stat --summary --find-copies-harder $oldrev${oldrev:+..}$newrev | skip_diff_tree_parent
350
351         if [ -n "$gitweburl" ]
352         then
353                 echo ""
354                 echo "$gitweburl?p=$reponame;a=commitdiff;h=$newrev${oldrev:+;hp=}$oldrev"
355         fi
356 }
357
358 print_change_info()
359 {
360         git rev-list -n 1 --pretty $1
361 }
362
363 #
364 # Called for the deletion of a branch
365 #
366 generate_delete_branch_email()
367 {
368         echo "       was  $oldrev"
369         echo ""
370         echo $LOGEND
371         git show -s --pretty=oneline $oldrev
372         echo $LOGEND
373 }
374
375 # --------------- Annotated tags
376
377 #
378 # Called for the creation of an annotated tag
379 #
380 generate_create_atag_email()
381 {
382         echo "        at  $newrev ($newrev_type)"
383
384         generate_atag_email
385 }
386
387 #
388 # Called for the update of an annotated tag (this is probably a rare event
389 # and may not even be allowed)
390 #
391 generate_update_atag_email()
392 {
393         echo "        to  $newrev ($newrev_type)"
394         echo "      from  $oldrev (which is now obsolete)"
395
396         generate_atag_email
397 }
398
399 #
400 # Called when an annotated tag is created or changed
401 #
402 generate_atag_email()
403 {
404         # Use git for-each-ref to pull out the individual fields from the
405         # tag
406         eval $(git for-each-ref --shell --format='
407         tagobject=%(*objectname)
408         tagtype=%(*objecttype)
409         tagger=%(taggername)
410         tagged=%(taggerdate)' $refname
411         )
412
413         echo "   tagging  $tagobject ($tagtype)"
414         case "$tagtype" in
415         commit)
416
417                 # If the tagged object is a commit, then we assume this is a
418                 # release, and so we calculate which tag this tag is
419                 # replacing
420                 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
421
422                 if [ -n "$prevtag" ]; then
423                         echo "  replaces  $prevtag"
424                 fi
425                 ;;
426         *)
427                 echo "    length  $(git cat-file -s $tagobject) bytes"
428                 ;;
429         esac
430         echo " tagged by  $tagger"
431         echo "        on  $tagged"
432
433         echo ""
434         echo $LOGBEGIN
435
436         # Show the content of the tag message; this might contain a change
437         # log or release notes so is worth displaying.
438         git cat-file tag $newrev | sed -e '1,/^$/d'
439
440         echo ""
441         case "$tagtype" in
442         commit)
443                 # Only commit tags make sense to have rev-list operations
444                 # performed on them
445                 if [ -n "$prevtag" ]; then
446                         # Show changes since the previous release
447                         git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
448                 else
449                         # No previous tag, show all the changes since time
450                         # began
451                         git rev-list --pretty=short $newrev | git shortlog
452                 fi
453                 ;;
454         *)
455                 # XXX: Is there anything useful we can do for non-commit
456                 # objects?
457                 ;;
458         esac
459
460         echo $LOGEND
461 }
462
463 #
464 # Called for the deletion of an annotated tag
465 #
466 generate_delete_atag_email()
467 {
468         echo "       was  $oldrev"
469         echo ""
470         echo $LOGEND
471         git show -s --pretty=oneline $oldrev
472         echo $LOGEND
473 }
474
475 # --------------- General references
476
477 #
478 # Called when any other type of reference is created (most likely a
479 # non-annotated tag)
480 #
481 generate_create_general_email()
482 {
483         echo "        at  $newrev ($newrev_type)"
484
485         generate_general_email
486 }
487
488 #
489 # Called when any other type of reference is updated (most likely a
490 # non-annotated tag)
491 #
492 generate_update_general_email()
493 {
494         echo "        to  $newrev ($newrev_type)"
495         echo "      from  $oldrev"
496
497         generate_general_email
498 }
499
500 #
501 # Called for creation or update of any other type of reference
502 #
503 generate_general_email()
504 {
505         # Unannotated tags are more about marking a point than releasing a
506         # version; therefore we don't do the shortlog summary that we do for
507         # annotated tags above - we simply show that the point has been
508         # marked, and print the log message for the marked point for
509         # reference purposes
510         #
511         # Note this section also catches any other reference type (although
512         # there aren't any) and deals with them in the same way.
513
514         echo ""
515         if [ "$newrev_type" = "commit" ]; then
516                 echo $LOGBEGIN
517                 git show --no-color --root -s --pretty=medium $newrev
518                 echo $LOGEND
519         else
520                 # What can we do here?  The tag marks an object that is not
521                 # a commit, so there is no log for us to display.  It's
522                 # probably not wise to output git cat-file as it could be a
523                 # binary blob.  We'll just say how big it is
524                 echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
525         fi
526 }
527
528 #
529 # Called for the deletion of any other type of reference
530 #
531 generate_delete_general_email()
532 {
533         echo "       was  $oldrev"
534         echo ""
535         echo $LOGEND
536         git show -s --pretty=oneline $oldrev
537         echo $LOGEND
538 }
539
540 send_mail()
541 {
542         if [ -n "$envelopesender" ]; then
543                 /usr/sbin/sendmail -t -f "$envelopesender"
544         else
545                 /usr/sbin/sendmail -t
546         fi
547 }
548
549 # ---------------------------- main()
550
551 # --- Constants
552 LOGBEGIN="- Log -----------------------------------------------------------------"
553 LOGEND="-----------------------------------------------------------------------"
554
555 # --- Config
556 # Set GIT_DIR either from the working directory, or from the environment
557 # variable.
558 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
559 if [ -z "$GIT_DIR" ]; then
560         echo >&2 "fatal: post-receive: GIT_DIR not set"
561         exit 1
562 fi
563
564 projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
565 # Check if the description is unchanged from it's default, and shorten it to
566 # a more manageable length if it is
567 if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
568 then
569         projectdesc="UNNAMED PROJECT"
570 fi
571
572 recipients=$(git config hooks.mailinglist)
573 announcerecipients=$(git config hooks.announcelist)
574 envelopesender=$(git config hooks.envelopesender)
575 emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')
576 gitweburl=$(git config hooks.gitweburl)
577 reponame=$(git config hooks.reponame || basename $(realpath $GIT_DIR))
578
579 # --- Main loop
580 # Allow dual mode: run from the command line just like the update hook, or
581 # if no arguments are given then run as a hook script
582 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
583         # Output to the terminal in command line mode - if someone wanted to
584         # resend an email; they could redirect the output to sendmail
585         # themselves
586         send_mail=cat
587         generate_email $2 $3 $1
588 else
589         while read oldrev newrev refname
590         do
591                 send_mail=send_mail
592                 generate_email $oldrev $newrev $refname
593         done
594 fi