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