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