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