update.sh: tolerate partial fetches
[girocco/readme.git] / taskd / mail.sh
blobac7def6ca1c5ff7b49571c629f659c2b417ec1a7
1 #!/bin/sh
3 # Copyright (c) 2007 Andy Parkins
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.
10 # =================
11 # This is Girocco-customized version. No matter what is said below, it has
12 # following changes:
13 # * Calling with arguments is same as giving them on stdin.
14 # * Optional fourth parameter is project name, used at most places instead
15 # of description.
16 # * Optional fifth parameter is email sender.
17 # * Optional sixth parameter is extra e-mail header to add.
18 # * If MAIL_SH_OTHER_BRANCHES is set it is taken as branch tips of already seen
19 # revisions when running show_new_revisions and mail.sh skips its own
20 # computation. If it starts with '@' the rest is the hash of a blob that
21 # contains the information (one tip per line).
22 # If MAIL_SH_OTHER_BRANCHES is not set, guess at what they are.
23 # MAIL_SH_OTHER_BRANCHES is ignored if updates are piped in via stdin.
24 # * Load shlib.
25 # * Default subject prefix is site name.
26 # * Unsubscribe instructions in email footer.
27 # * Default showrev includes gitweb link and show -C.
28 # * Nicer subject line.
29 # * Limit mail size to 256kb.
30 # =================
32 # This hook is stored in the contrib/hooks directory. Your distribution
33 # will have put this somewhere standard. You should make this script
34 # executable then link to it in the repository you would like to use it in.
35 # For example, on debian the hook is stored in
36 # /usr/share/git-core/contrib/hooks/post-receive-email:
38 # chmod a+x post-receive-email
39 # cd /path/to/your/repository.git
40 # ln -sf /usr/share/git-core/contrib/hooks/post-receive-email hooks/post-receive
42 # This hook script assumes it is enabled on the central repository of a
43 # project, with all users pushing only to it and not between each other. It
44 # will still work if you don't operate in that style, but it would become
45 # possible for the email to be from someone other than the person doing the
46 # push.
48 # Config
49 # ------
50 # hooks.mailinglist
51 # This is the list that all pushes will go to; leave it blank to not send
52 # emails for every ref update.
53 # hooks.reverseorder
54 # If true new revisions are shown in reverse order (i.e. oldest to newest)
55 # hooks.summaryonly
56 # If true do not include the actual diff when showing new commits (the
57 # summary information will still be shown). Default is false.
58 # hooks.announcelist
59 # This is the list that all pushes of annotated tags will go to. Leave it
60 # blank to default to the mailinglist field. The announce emails lists
61 # the short log summary of the changes since the last annotated tag.
62 # hooks.envelopesender
63 # If set then the -f option is passed to sendmail to allow the envelope
64 # sender address to be set
65 # hooks.emailprefix
66 # All emails have their subjects prefixed with this prefix, or "[SCM]"
67 # if emailprefix is unset, to aid filtering
68 # hooks.showrev
69 # The shell command used to format each revision in the email, with
70 # "%s" replaced with the commit id. Defaults to "git rev-list -1
71 # --pretty %s", displaying the commit id, author, date and log
72 # message. To list full patches separated by a blank line, you
73 # could set this to "git show -C %s; echo".
74 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
75 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
76 # Be careful if "..." contains things that will be expanded by shell "eval"
77 # or printf.
79 # Notes
80 # -----
81 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
82 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
83 # give information for debugging.
86 # ---------------------------- Functions
89 # Function to output arguments without interpretation followed by \n
91 echol()
93 printf '%s\n' "$*"
97 # Function to read a tag's (the single argument) header fields into
98 # tagobject the "object" value
99 # tagtype the "type" value
100 # tagtag the "tag" value
101 # taggername from "tagger" value before first '<'
102 # taggerdate from "tagger" value after '>' but formatted into a date string
103 # taggeremail from "tagger" value between '<' and '>'
104 # On return all fields will be set (to empty if failure) and the three tagger
105 # fields will only be set to non-empty if a "tagger" header field is present.
106 # If the object does not exist or is not a tag the function returns failure.
107 read_tag_fields()
109 tagobject=
110 tagtype=
111 tagtag=
112 taggername=
113 taggerdate=
114 taggeremail=
115 if _tagfields="$(git cat-file tag "$1" | LC_ALL=C awk -F '[ ]' '
116 function quotesh(sv) {
117 gsub(/'\''/, "'\'\\\\\'\''", sv)
118 return "'\''" sv "'\''"
120 function trim(sv) {
121 sub(/^[ \t]+/, "", sv)
122 sub(/[ \t]+$/, "", sv)
123 return sv
125 function hval(sv) {
126 sub(/^[^ ]* /, "", sv)
127 return sv
129 NR==1,/^$/ {
130 if ($1 == "object") to=hval($0)
131 if ($1 == "type") ty=hval($0)
132 if ($1 == "tag") tt=hval($0)
133 if ($1 == "tagger") tr=hval($0)
135 END {
136 if (tt == "" || to == "" || ty == "") exit 1
137 print "tagobject=" quotesh(to)
138 print "tagtype=" quotesh(ty)
139 print "tagtag=" quotesh(tt)
140 if (match(tr, /^[ \t]*[^ \t<][^<]*/)) {
141 tn=substr(tr, RSTART, RLENGTH)
142 tr=substr(tr, RSTART + RLENGTH)
143 tn=trim(tn)
144 if (tn != "")
145 print "taggername=" quotesh(tn)
146 if (match(tr, /^<.*>/)) {
147 if (RLENGTH > 2)
148 te=substr(tr, RSTART+1, RLENGTH-2)
149 tr=substr(tr, RSTART + RLENGTH)
150 te=trim(te)
151 if (te != "")
152 print "taggeremail=" quotesh(te)
153 if (match(tr, /^[ \t]*[0-9]+([ \t]+[-+]?[0-9][0-9]([0-9][0-9]([0-9][0-9])?)?)[ \t]*$/))
154 td=trim(tr)
155 if (td != "")
156 print "taggerdate=" quotesh(td)
160 ')"; then
161 eval "$_tagfields"
162 [ -z "$taggerdate" ] || taggerdate="$(strftime "$sdatefmt" $taggerdate)"
163 return 0
165 return 1
169 # Function to prepare for email generation. This decides what type
170 # of update this is and whether an email should even be generated.
172 prep_for_email()
174 # --- Arguments
175 oldrev="$(git rev-parse --revs-only "$1" --)" || :
176 newrev="$(git rev-parse --revs-only "$2" --)" || :
177 [ -n "$oldrev" ] && [ -n "$newrev" ] && [ "$oldrev" != "$newrev" ] || return 1
178 scratch="${newrev#????????????????}"
179 newrev16="${newrev%$scratch}"
180 refname="$3"
181 refname_type=
183 # --- Interpret
184 # 0000->1234 (create)
185 # 1234->2345 (update)
186 # 2345->0000 (delete)
187 if [ -n "$oldrev" ] && [ "${oldrev#*[!0]}" = "$oldrev" ]
188 then
189 change_type="create"
190 else
191 if [ -n "$newrev" ] && [ "${newrev#*[!0]}" = "$newrev" ]
192 then
193 change_type="delete"
194 else
195 change_type="update"
199 # --- Get the revision types
200 newrev_type="$(git cat-file -t "$newrev" 2>/dev/null)" || :
201 oldrev_type="$(git cat-file -t "$oldrev" 2>/dev/null)" || :
202 case "$change_type" in
203 create|update)
204 rev="$newrev"
205 rev_type="$newrev_type"
207 delete)
208 rev="$oldrev"
209 rev_type="$oldrev_type"
211 esac
213 # The revision type tells us what type the commit is, combined with
214 # the location of the ref we can decide between
215 # - working branch
216 # - tracking branch
217 # - unannoted tag
218 # - annotated tag
219 case "$refname:$rev_type" in
220 refs/tags/*:commit)
221 # un-annotated tag
222 refname_type="tag"
223 short_refname="${refname#refs/tags/}"
225 refs/tags/*:tag)
226 # annotated tag
227 refname_type="annotated tag"
228 short_refname="${refname#refs/tags/}"
229 # change recipients
230 if [ -n "$announcerecipients" ]; then
231 recipients="$announcerecipients"
234 refs/heads/*:commit|refs/heads/*:tag)
235 # branch
236 refname_type="branch"
237 short_refname="${refname#refs/heads/}"
239 refs/remotes/*:commit|refs/remotes/*:tag)
240 # tracking branch
241 refname_type="tracking branch"
242 short_refname="${refname#refs/remotes/}"
243 echol >&2 "*** Push-update of tracking branch, $refname"
244 echol >&2 "*** - no email generated."
245 return 1
247 refs/mob/*:commit|refs/mob/*:tag)
248 # personal mob ref
249 refname_type="personal mob ref"
250 short_refname="${refname#refs/mob/}"
251 echol >&2 "*** Push-update of personal mob ref, $refname"
252 echol >&2 "*** - no email generated."
253 return 1
256 # Anything else (is there anything else?)
257 echol >&2 "*** Unknown type of update to $refname ($rev_type)"
258 echol >&2 "*** - no email generated"
259 return 1
261 esac
263 # Check if we've got anyone to send to
264 if [ -z "$recipients" ]; then
265 case "$refname_type" in
266 "annotated tag")
267 config_name="neither hooks.announcelist nor hooks.mailinglist is"
270 config_name="hooks.mailinglist is not"
272 esac
273 echol >&2 "*** $config_name set so no email will be sent"
274 echol >&2 "*** for $refname update $oldrev->$newrev"
275 return 1
278 return 0
282 # Top level email generation function. This calls the appropriate
283 # body-generation routine after outputting the common header.
285 # Note this function doesn't actually generate any email output, that is
286 # taken care of by the functions it calls:
287 # - generate_email_header
288 # - generate_create_XXXX_email
289 # - generate_update_XXXX_email
290 # - generate_delete_XXXX_email
291 # - generate_email_footer
293 # Note also that this function cannot 'exit' from the script; when this
294 # function is running (in hook script mode), the send_mail() function
295 # is already executing in another process, connected via a pipe, and
296 # if this function exits without, whatever has been generated to that
297 # point will be sent as an email... even if nothing has been generated.
299 generate_email()
301 # Email parameters
302 # The email subject will contain the best description of the ref
303 # that we can build from the parameters
304 describe="$(git describe $rev 2>/dev/null)" || :
305 if [ -z "$describe" ]; then
306 describe="$rev"
309 generate_email_header
311 # Call the correct body generation function
312 fn_name=general
313 case "$refname_type" in
314 "tracking branch"|branch)
315 fn_name=branch
317 "annotated tag")
318 fn_name=atag
320 esac
321 generate_${change_type}_${fn_name}_email
323 generate_email_footer
326 generate_email_header()
328 # --- Email (all stdout will be the email)
329 # Generate header
330 if [ -n "$emailsender" ]; then
331 echol "From: $emailsender"
333 cat <<-EOF
334 To: $recipients
335 Subject: ${emailprefix}$projectname $refname_type $short_refname ${change_type}d: $describe
336 MIME-Version: 1.0
337 Content-Type: text/plain; charset=UTF-8
338 Content-Transfer-Encoding: 8bit
340 [ -z "$refname" ] || echol "References: <$refname@$projurl>"
341 [ -n "$cfg_suppress_x_girocco" ] || echol "X-Girocco: $cfg_gitweburl"
342 [ -z "$emailextraheader" ] || echol "$emailextraheader"
343 cat <<-EOF
344 X-Git-Refname: $refname
345 X-Git-Reftype: $refname_type
346 X-Git-Oldrev: $oldrev
347 X-Git-Newrev: $newrev
348 Auto-Submitted: auto-generated
350 This is an automated email generated because a ref change occurred in the
351 git repository for project $projectname.
353 The $refname_type, $short_refname has been ${change_type}d
357 generate_email_footer()
359 SPACE=" "
360 cat <<-EOF
363 ${cfg_name:-mail.sh} automatic notification. Contact project admin $projectowner
364 if you want to unsubscribe, or site admin ${cfg_admin:+$cfg_admin }if you receive
365 no reply.
366 --${SPACE}
367 $projectboth
371 # --------------- Branches
374 # Called for the creation of a branch
376 generate_create_branch_email()
378 # This is a new branch and so oldrev is not valid
379 echol " at $newrev ($newrev_type)"
380 echol ""
382 show_new_revisions_eval='
383 echol "Those revisions in this branch that are new to this repository"
384 echol "have not appeared on any other notification email; so we list"
385 echol "those revisions in full, below."
387 echol ""
388 echol "$LOGBEGIN"
390 show_new_revisions
391 if [ -n "$LAST_SHOWN_REVISION" ]; then
392 echol "$LOGEND"
393 else
394 echol "No new revisions were added by this branch."
397 # If any revisions were shown by show_new_revisions then we show
398 # a diffstat from the last shown revisions's parent (or the empty tree
399 # if the last revision shown is a root revision) to the new revision
400 # provided the last shown revision is not a merge commit and there
401 # were no more boundary commits encountered by show_new_revisions than
402 # the last shown revision has parents.
403 # This is to show the truth of what happened in this change.
405 if [ -n "$LAST_SHOWN_REVISION" ] && [ -n "$LAST_SHOWN_NPARENTS" ] && [ -n "$LAST_SHOWN_NBOUNDARY" ] &&
406 [ "$LAST_SHOWN_NPARENTS" -le 1 ] && [ "$LAST_SHOWN_NBOUNDARY" -le "$LAST_SHOWN_NPARENTS" ]; then
407 if [ "$LAST_SHOWN_NPARENTS" -eq 0 ]; then
408 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
409 # whether or not it's actually present in the repository.
411 # Set oldrev to the result of $(git hash-object -t tree --stdin </dev/null)
412 oldrev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
413 else
414 # Set oldrev to the parent of the last shown revision
415 oldrev="$LAST_SHOWN_REVISION^"
417 echol ""
418 echol "Summary of changes:"
419 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder "$oldrev" "$newrev" --
424 # Called for the change of a pre-existing branch
426 generate_update_branch_email()
428 # Consider this:
429 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
431 # O is $oldrev for $refname
432 # N is $newrev for $refname
433 # X is a revision pointed to by some other ref, for which we may
434 # assume that an email has already been generated.
435 # In this case we want to issue an email containing only revisions
436 # 3, 4, and N. Given (almost) by
438 # git rev-list N ^O --not --all
440 # The reason for the "almost", is that the "--not --all" will take
441 # precedence over the "N", and effectively will translate to
443 # git rev-list N ^O ^X ^N
445 # So, we need to build up the list more carefully. git rev-parse
446 # will generate a list of revs that may be fed into git rev-list.
447 # We can get it to make the "--not --all" part and then filter out
448 # the "^N" with:
450 # git rev-parse --not --all | grep -v N
452 # Then, using the --stdin switch to git rev-list we have effectively
453 # manufactured
455 # git rev-list N ^O ^X
457 # This leaves a problem when someone else updates the repository
458 # while this script is running. Their new value of the ref we're
459 # working on would be included in the "--not --all" output; and as
460 # our $newrev would be an ancestor of that commit, it would exclude
461 # all of our commits. What we really want is to exclude the current
462 # value of $refname from the --not list, rather than N itself. So:
464 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
466 # Get's us to something pretty safe (apart from the small time
467 # between refname being read, and git rev-parse running - for that,
468 # I give up)
471 # Next problem, consider this:
472 # * --- B --- * --- O ($oldrev)
474 # * --- X --- * --- N ($newrev)
476 # That is to say, there is no guarantee that oldrev is a strict
477 # subset of newrev (it would have required a --force, but that's
478 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
479 # Instead we find the common base of the two revs and list from
480 # there.
482 # As above, we need to take into account the presence of X; if
483 # another branch is already in the repository and points at some of
484 # the revisions that we are about to output - we don't want them.
485 # The solution is as before: git rev-parse output filtered.
487 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
489 # Tags pushed into the repository generate nice shortlog emails that
490 # summarise the commits between them and the previous tag. However,
491 # those emails don't include the full commit messages that we output
492 # for a branch update. Therefore we still want to output revisions
493 # that have been output on a tag email.
495 # Luckily, git rev-parse includes just the tool. Instead of using
496 # "--all" we use "--branches"; this has the added benefit that
497 # "remotes/" will be ignored as well.
499 # List all of the revisions that were removed by this update, in a
500 # fast-forward update, this list will be empty, because rev-list O
501 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
502 # revisions
503 fast_forward=
504 rev=
505 for rev in $(git rev-list "$newrev..$oldrev" --)
507 revtype="$(git cat-file -t "$rev")" || :
508 echol " discards $rev ($revtype)"
509 done
510 if [ -z "$rev" ]; then
511 fast_forward=1
514 # List all the revisions from baserev to newrev in a kind of
515 # "table-of-contents"; note this list can include revisions that
516 # have already had notification emails and is present to show the
517 # full detail of the change from rolling back the old revision to
518 # the base revision and then forward to the new revision
519 for rev in $(git rev-list "$oldrev..$newrev" --)
521 revtype="$(git cat-file -t "$rev")" || :
522 echol " via $rev ($revtype)"
523 done
525 if [ "$fast_forward" ]; then
526 echol " from $oldrev ($oldrev_type)"
527 else
528 # 1. Existing revisions were removed. In this case newrev
529 # is a subset of oldrev - this is the reverse of a
530 # fast-forward, a rewind
531 # 2. New revisions were added on top of an old revision,
532 # this is a rewind and addition.
534 # (1) certainly happened, (2) possibly. When (2) hasn't
535 # happened, we set a flag to indicate that no log printout
536 # is required.
538 echol ""
540 # Find the common ancestor of the old and new revisions and
541 # compare it with newrev
542 baserev=$(git merge-base $oldrev $newrev)
543 rewind_only=
544 if [ "$baserev" = "$newrev" ]; then
545 echol "This update discarded existing revisions and left the branch pointing at"
546 echol "a previous point in the repository history."
547 echol ""
548 echol " * -- * -- N ($newrev)"
549 echol " \\"
550 echol " O -- O -- O ($oldrev)"
551 echol ""
552 echol "The removed revisions are not necessarily gone - if another reference"
553 echol "still refers to them they will stay in the repository."
554 rewind_only=1
555 else
556 echol "This update added new revisions after undoing existing revisions. That is"
557 echol "to say, the old revision is not a strict subset of the new revision. This"
558 echol "situation occurs when you --force push a change and generate a repository"
559 echol "containing something like this:"
560 echol ""
561 echol " * -- * -- B -- O -- O -- O ($oldrev)"
562 echol " \\"
563 echol " N -- N -- N ($newrev)"
564 echol ""
565 echol "When this happens we assume that you've already had alert emails for all"
566 echol "of the O revisions, and so we here report only the revisions in the N"
567 echol "branch from the common base, B."
571 echol ""
572 LAST_SHOWN_REVISION=
573 if [ -z "$rewind_only" ]; then
574 show_new_revisions_eval='
575 echol "Those revisions listed above that are new to this repository have"
576 echol "not appeared on any other notification email; so we list those"
577 echol "revisions in full, below."
579 echol ""
580 echol "$LOGBEGIN"
582 show_new_revisions
584 if [ -n "$LAST_SHOWN_REVISION" ]; then
585 echol "$LOGEND"
586 else
587 echol "No new revisions were added by this update."
590 # The diffstat is shown from the old revision to the new revision.
591 # This is to show the truth of what happened in this change.
592 # There's no point showing the stat from the base to the new
593 # revision because the base is effectively a random revision at this
594 # point - the user will be interested in what this revision changed
595 # - including the undoing of previous revisions in the case of
596 # non-fast-forward updates.
597 echol ""
598 echol "Summary of changes:"
599 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder "$oldrev..$newrev" --
603 # Called for the deletion of a branch
605 generate_delete_branch_email()
607 echol " was $oldrev"
608 echol ""
609 echol "$LOGBEGIN"
610 git diff-tree --no-color --date="$datefmt" -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline "$oldrev" --
611 echol "$LOGEND"
614 # --------------- Annotated tags
617 # Called for the creation of an annotated tag
619 generate_create_atag_email()
621 echol " at $newrev ($newrev_type)"
623 generate_atag_email
627 # Called for the update of an annotated tag (this is probably a rare event
628 # and may not even be allowed)
630 generate_update_atag_email()
632 echol " to $newrev ($newrev_type)"
633 echol " from $oldrev (which is now obsolete)"
635 generate_atag_email
639 # Called when an annotated tag is created or changed
641 generate_atag_email()
643 # Use read_tag_fields to pull out the individual fields from the
644 # tag and git cat-file --batch-check to fully peel the tag if needed
645 tagrev="$newrev"
646 tagrev16="$newrev16"
647 read_tag_fields "$tagrev"
648 if [ "$tagtype" = "tag" ]; then
649 # fully peel the tag
650 peeledobject=
651 peeledtype=
652 _po=
653 _pt=
654 read -r _po _pt junk <<-EOT || :
655 $(git cat-file --batch-check"${var_have_git_185:+=%(objectname) %(objecttype)}" <<-ETX || :
656 $tagrev^{}
660 if [ -n "$_po" ] && [ -n "$_pt" ] && [ "$_pt" != "missing" ] && [ "$_po" != "missing" ]; then
661 peeledobject="$_po"
662 peeledtype="$_pt"
663 else
664 peeledtype="missing"
666 else
667 peeledobject="$tagobject"
668 peeledtype="$tagtype"
671 echol " tagging $peeledobject ($peeledtype)"
672 case "$peeledtype" in
673 commit)
675 # If the tagged object is a commit, then we assume this is a
676 # release, and so we calculate which tag this tag is
677 # replacing
678 prevtag="$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)" || :
680 if [ -n "$prevtag" ]; then
681 echol " replaces $prevtag"
685 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
687 esac
688 echol " tagged by $taggername"
689 echol " on $taggerdate"
691 echol ""
692 echol "$LOGBEGIN"
694 while
695 echol "tag $tagrev"
696 echol "Tag: $tagtag"
697 echol "Object: $tagobject ($tagtype)"
698 [ -z "$taggername" ] ||
699 echol "Tagger: $taggername"
700 [ -z "$taggerdate" ] ||
701 echol "Date: $taggerdate"
702 echol "URL: <$projurl/$tagrev16>"
703 echol ""
705 # Show the content of the tag message; this might contain a change
706 # log or release notes so is worth displaying.
707 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
708 echol ""
709 [ "$tagtype" = "tag" ]
711 tagrev="$tagobject"
712 scratch="${tagrev#????????????????}"
713 tagrev16="${tagrev%$scratch}"
714 read_tag_fields "$tagrev" || break
715 done
717 case "$peeledtype" in
718 commit)
719 # Only commit tags make sense to have rev-list operations
720 # performed on them
721 if [ -n "$prevtag" ]; then
722 # Show changes since the previous release
723 git shortlog "$prevtag..$newrev" --
724 else
725 # No previous tag, show all the changes since time
726 # began
727 git shortlog "$newrev" --
731 # XXX: Is there anything useful we can do for non-commit
732 # objects?
734 esac
736 echol "$LOGEND"
740 # Called for the deletion of an annotated tag
742 generate_delete_atag_email()
744 echol " was $oldrev"
745 echol ""
746 echol "$LOGBEGIN"
747 git diff-tree --no-color --date="$datefmt" -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline "$oldrev" --
748 echol "$LOGEND"
751 # --------------- General references
754 # Called when any other type of reference is created (most likely a
755 # non-annotated tag)
757 generate_create_general_email()
759 echol " at $newrev ($newrev_type)"
761 generate_general_email
765 # Called when any other type of reference is updated (most likely a
766 # non-annotated tag)
768 generate_update_general_email()
770 echol " to $newrev ($newrev_type)"
771 echol " from $oldrev"
773 generate_general_email
777 # Called for creation or update of any other type of reference
779 generate_general_email()
781 # Unannotated tags are more about marking a point than releasing a
782 # version; therefore we don't do the shortlog summary that we do for
783 # annotated tags above - we simply show that the point has been
784 # marked, and print the log message for the marked point for
785 # reference purposes
787 # Note this section also catches any other reference type (although
788 # there aren't any) and deals with them in the same way.
790 echol ""
791 if [ "$newrev_type" = "commit" ]; then
792 if [ -n "$(git rev-list --no-walk --merges "$newrev" --)" ]; then
793 pfmt12="$pfmt1$pfmt1m$pfmt2"
794 else
795 pfmt12="$pfmt1$pfmt2"
797 echol "$LOGBEGIN"
798 git diff-tree --no-color --date="$datefmt" --root -s --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$newrev16$pfmt3" --abbrev=$habbr "$newrev" --
799 echol "$LOGEND"
800 else
801 # What can we do here? The tag marks an object that is not
802 # a commit, so there is no log for us to display. It's
803 # probably not wise to output git cat-file as it could be a
804 # binary blob. We'll just say how big it is
805 echol "$newrev is a $newrev_type, and is $(git cat-file -s "$newrev") bytes long."
810 # Called for the deletion of any other type of reference
812 generate_delete_general_email()
814 echol " was $oldrev"
815 echol ""
816 echol "$LOGBEGIN"
817 git diff-tree --no-color --date="$datefmt" -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline "$oldrev" --
818 echol "$LOGEND"
822 # --------------- Miscellaneous utilities
825 # Show new revisions as the user would like to see them in the email.
827 # On return LAST_SHOWN_REVISION will be set to non-empty if any revisions shown
828 # and LAST_SHOWN_NPARENTS will be the number of parents it has (possibly 0)
829 # and LAST_SHOWN_NBOUNDARY will be the total number of boundary commits seen (possibly 0)
830 # So if $LAST_SHOWN_NBOUNDARY is greater than $LAST_SHOWN_NPARENTS then the
831 # portion of the graph shown by show_new_revisions (excluding $LAST_SHOWN_REVISION)
832 # includes at least one merge commit that had at least one parent excluded.
833 # If show_new_revisions_eval is non-empty, than just before the first output
834 # gets shown it will be eval'd and then set to "".
836 show_new_revisions()
838 # This shows all log entries that are not already covered by
839 # another ref - i.e. commits that are now accessible from this
840 # ref that were previously not accessible
841 # (see generate_update_branch_email for the explanation of this
842 # command)
843 LAST_SHOWN_REVISION=
844 LAST_SHOWN_NPARENTS=
845 LAST_SHOWN_NBOUNDARY=0
847 # Revision range passed to rev-list differs for new vs. updated
848 # branches.
849 if [ "$change_type" = create ]
850 then
851 # Show all revisions exclusive to this (new) branch.
852 revspec="$newrev"
853 else
854 # Branch update; show revisions not part of $oldrev.
855 revspec="$oldrev..$newrev"
858 if [ "${MAIL_SH_OTHER_BRANCHES+set}" = "set" ]; then
859 case "$MAIL_SH_OTHER_BRANCHES" in
861 othertips="git cat-file blob '${MAIL_SH_OTHER_BRANCHES#@}' 2>/dev/null"
864 othertips='printf "%s\n" $MAIL_SH_OTHER_BRANCHES'
866 esac
867 else
868 othertips='git for-each-ref --format="%(refname)" refs/heads |
869 LC_ALL=C awk -v "refname=$refname" "\$1 != refname"'
871 # if [ -z "$custom_showrev" ]
872 # then
873 # git rev-list --pretty --stdin "$revspec" --
874 # else
875 while read onerev mark pcnt && [ -n "$onerev" ] && [ -n "$mark" ] && [ -n "$pcnt" ]
877 if [ "$mark" = "-" ]; then
878 LAST_SHOWN_NBOUNDARY=$(( $LAST_SHOWN_NBOUNDARY + 1 ))
879 continue
881 if [ -n "$show_new_revisions_eval" ]; then
882 eval "$show_new_revisions_eval"
883 show_new_revisions_eval=
885 if [ -z "$reverseopt" ] || [ -z "$LAST_SHOWN_REVISION" ]; then
886 LAST_SHOWN_REVISION="$onerev"
887 LAST_SHOWN_NPARENTS="$pcnt"
889 if [ -n "$custom_showrev" ]; then
890 eval $(printf "$custom_showrev" $onerev)
891 else
892 if [ "${summaryonly:-false}" = "false" ]; then
893 if [ ${pcnt:-1} -gt 1 ]; then
894 opts="-p --cc"
895 pfmt12="$pfmt1$pfmt1m$pfmt2"
896 else
897 opts="-p --stat=72 --summary"
898 pfmt12="$pfmt1$pfmt2"
900 else
901 if [ ${pcnt:-1} -gt 1 ]; then
902 opts="-s"
903 pfmt12="$pfmt1$pfmt1m$pfmt2"
904 else
905 opts="--stat=72 --summary"
906 pfmt12="$pfmt1$pfmt2"
909 scratch="${onerev#????????????????}"
910 onerev16="${onerev%$scratch}"
911 git diff-tree --no-color --date="$datefmt" $opts --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$onerev16$pfmt3" --abbrev=$habbr -B -C --root "$onerev" --
912 echo
914 done <<EOT
915 $(eval "$othertips" |
916 git cat-file --batch-check"${var_have_git_185:+=%(objectname)}" |
917 LC_ALL=C sed -e '/ missing$/d' -e 's/^\([^ ][^ ]*\).*$/\1/' -e 's/^/^/' |
918 git --no-pager log --stdin --no-color $reverseopt --boundary --format=tformat:"%H %m %p" "$revspec" -- |
919 LC_ALL=C awk '{print $1 " " $2 " " NF-2}')
921 # fi
925 size_limit()
927 size=0
928 while IFS= read -r line; do
929 # Include size of RFC 822 trailing CR+LF on each line
930 size=$(($size+${#line}+2))
931 if [ $size -gt $1 ]; then
932 echol "...e-mail trimmed, has been too large."
933 break
935 echol "$line"
936 done
940 sendmail_to_stdout()
946 send_mail()
948 if [ -n "$cfg_sender" ]; then
949 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
950 else
951 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
955 # ---------------------------- main()
957 # --- Constants
958 LOGBEGIN="- Log -----------------------------------------------------------------"
959 LOGEND="-----------------------------------------------------------------------"
961 # --- Config
962 ! [ -f @basedir@/shlib.sh ] || ! [ -r @basedir@/shlib.sh ] || . @basedir@/shlib.sh
964 # Git formatting to use
965 datefmt=rfc2822
966 habbr=12
968 # strftime formatting to use (empty is default rfc2822 format)
969 sdatefmt=
971 # This is --pretty=medium in four parts with a URL: line added
972 # The pfmt1m value must be inserted after the pfmt1 value but only for merges
973 # The URL must be inserted between pfmt2 and pfmt3
974 pfmt1='format:commit %H%n'
975 pfmt1m='Merge: %p%n'
976 pfmt2='Author: %an <%ae>%nDate: %ad%nURL: <'
977 pfmt3='>%n%n%w(0,4,4)%s%n%n%b'
979 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'core.abbrev=$habbr'"
980 export GIT_CONFIG_PARAMETERS
982 # Set GIT_DIR either from the working directory, or from the environment
983 # variable.
984 GIT_DIR="$(git rev-parse --git-dir 2>/dev/null)" || :
985 if [ -z "$GIT_DIR" ]; then
986 echol >&2 "fatal: mail.sh: GIT_DIR not set"
987 exit 1
989 # Get GIT_COMMON_DIR for Git >= 2.5.0
990 GCD_DIR="$(cd "$GIT_DIR" && unset GIT_DIR && git rev-parse --git-common-dir 2>/dev/null)" || :
991 if [ -z "$GCD_DIR" ] || [ "$GCD_DIR" = "--git-common-dir" ]; then
992 # Must be pre-2.5.0
993 GCD_DIR="$GIT_DIR"
994 else
995 # Might be relative to GIT_DIR so cd there first
996 GCD_DIR="$(cd "$GIT_DIR" && cd "$GCD_DIR" && pwd)"
999 projectdesc=
1000 ! [ -s "$GCD_DIR/description" ] || projectdesc="$(LC_ALL=C sed -ne '1p' "$GCD_DIR/description")"
1001 # Check if the description is unchanged from it's default, and shorten it to
1002 # a more manageable length if it is
1003 case "$projectdesc" in ""|"Unnamed repository"*)
1004 projectdesc="UNNAMED PROJECT"
1005 esac
1007 # If --stdout is first argument send all output there instead of mailing
1008 if [ "$1" = "--stdout" ]; then
1009 shift
1010 cfg_sendmail_bin="sendmail_to_stdout"
1013 projectname="${4%.git}"
1014 if [ -n "$projectname" ]; then
1015 projectname="$projectname.git"
1016 projectowner="$(git config gitweb.owner 2>/dev/null)" || :
1017 else
1018 projectname="$(cd "$GIT_DIR" && pwd)"
1019 projectname="${projectname%/.git}"
1020 projectname="$(basename "$projectname")"
1022 projectboth="$projectname (\"$projectdesc\")"
1023 projurl="$cfg_gitweburl/$projectname"
1024 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
1026 emailsender="$5"
1027 emailextraheader="$6"
1029 recipients="$(git config hooks.mailinglist 2>/dev/null)" || :
1030 summaryonly="$(git config --bool hooks.summaryonly 2>/dev/null)" || :
1031 reverseopt=
1032 [ "$(git config --bool hooks.reverseorder 2>/dev/null || :)" != "true" ] || reverseopt=--reverse
1033 announcerecipients="$(git config hooks.announcelist)" || :
1034 envelopesender="$(git config hooks.envelopesender)" || :
1035 emailprefix="$(git config hooks.emailprefix || echol "[${cfg_name:-mail.sh}] ")" || :
1036 custom_showrev="$(git config hooks.showrev)" || :
1038 # --- Main loop
1039 # Allow dual mode: run from the command line just like the update hook, or
1040 # if no arguments are given then run as a hook script
1041 # If --stdout is first argument send all output there instead (handled above)
1042 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
1043 if [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ]; then
1044 # Handle a single update rather than a batch on stdin
1045 # Output will still be sent to sendmail unless --stdout is used
1046 # Same 3 args as update hook (<refname> <old> <new>)
1047 if prep_for_email $2 $3 $1; then
1048 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1050 else
1051 # MAIL_SH_OTHER_BRANCHES cannot possibly be valid for multiple updates
1052 unset MAIL_SH_OTHER_BRANCHES
1053 # Same input as pre-receive hook (each line is <old> <new> <refname>)
1054 while read oldrev newrev refname
1056 prep_for_email $oldrev $newrev $refname || continue
1057 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1058 done
1061 exit 0