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