mail.sh: avoid use of "post-receive" in messages
[girocco.git] / taskd / mail.sh
blob21e834ec05ac4231be439ef178186658c642691b
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.summaryonly
54 # If true do not include the actual diff when showing new commits (the
55 # summary information will still be shown). Default is false.
56 # hooks.announcelist
57 # This is the list that all pushes of annotated tags will go to. Leave it
58 # blank to default to the mailinglist field. The announce emails lists
59 # the short log summary of the changes since the last annotated tag.
60 # hooks.envelopesender
61 # If set then the -f option is passed to sendmail to allow the envelope
62 # sender address to be set
63 # hooks.emailprefix
64 # All emails have their subjects prefixed with this prefix, or "[SCM]"
65 # if emailprefix is unset, to aid filtering
66 # hooks.showrev
67 # The shell command used to format each revision in the email, with
68 # "%s" replaced with the commit id. Defaults to "git rev-list -1
69 # --pretty %s", displaying the commit id, author, date and log
70 # message. To list full patches separated by a blank line, you
71 # could set this to "git show -C %s; echo".
72 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
73 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
74 # Be careful if "..." contains things that will be expanded by shell "eval"
75 # or printf.
77 # Notes
78 # -----
79 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
80 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
81 # give information for debugging.
84 # ---------------------------- Functions
87 # Function to output arguments without interpretation followed by \n
89 echol()
91 printf '%s\n' "$*"
95 # Function to read a tag's (the single argument) header fields into
96 # tagobject the "object" value
97 # tagtype the "type" value
98 # tagtag the "tag" value
99 # taggername from "tagger" value before first '<'
100 # taggerdate from "tagger" value after '>' but formatted into a date string
101 # taggeremail from "tagger" value between '<' and '>'
102 # On return all fields will be set (to empty if failure) and the three tagger
103 # fields will only be set to non-empty if a "tagger" header field is present.
104 # If the object does not exist or is not a tag the function returns failure.
105 read_tag_fields()
107 tagobject=
108 tagtype=
109 tagtag=
110 taggername=
111 taggerdate=
112 taggeremail=
113 if _tagfields="$(git cat-file tag "$1" | LC_ALL=C awk -F '[ ]' '
114 function quotesh(sv) {
115 gsub(/'\''/, "'\'\\\\\'\''", sv)
116 return "'\''" sv "'\''"
118 function trim(sv) {
119 sub(/^[ \t]+/, "", sv)
120 sub(/[ \t]+$/, "", sv)
121 return sv
123 function hval(sv) {
124 sub(/^[^ ]* /, "", sv)
125 return sv
127 NR==1,/^$/ {
128 if ($1 == "object") to=hval($0)
129 if ($1 == "type") ty=hval($0)
130 if ($1 == "tag") tt=hval($0)
131 if ($1 == "tagger") tr=hval($0)
133 END {
134 if (tt == "" || to == "" || ty == "") exit 1
135 print "tagobject=" quotesh(to)
136 print "tagtype=" quotesh(ty)
137 print "tagtag=" quotesh(tt)
138 if (match(tr, /^[ \t]*[^ \t<][^<]*/)) {
139 tn=substr(tr, RSTART, RLENGTH)
140 tr=substr(tr, RSTART + RLENGTH)
141 tn=trim(tn)
142 if (tn != "")
143 print "taggername=" quotesh(tn)
144 if (match(tr, /^<.*>/)) {
145 if (RLENGTH > 2)
146 te=substr(tr, RSTART+1, RLENGTH-2)
147 tr=substr(tr, RSTART + RLENGTH)
148 te=trim(te)
149 if (te != "")
150 print "taggeremail=" quotesh(te)
151 if (match(tr, /^[ \t]*[0-9]+([ \t]+[-+]?[0-9][0-9]([0-9][0-9]([0-9][0-9])?)?)[ \t]*$/))
152 td=trim(tr)
153 if (td != "")
154 print "taggerdate=" quotesh(td)
158 ')"; then
159 eval "$_tagfields"
160 [ -z "$taggerdate" ] || taggerdate="$(strftime "$sdatefmt" $taggerdate)"
161 return 0
163 return 1
167 # Function to prepare for email generation. This decides what type
168 # of update this is and whether an email should even be generated.
170 prep_for_email()
172 # --- Arguments
173 oldrev=$(git rev-parse --revs-only "$1" --)
174 newrev=$(git rev-parse --revs-only "$2" --)
175 [ "$oldrev" != "$newrev" ] || return 1
176 scratch="${newrev#????????????????}"
177 newrev16="${newrev%$scratch}"
178 refname="$3"
180 # --- Interpret
181 # 0000->1234 (create)
182 # 1234->2345 (update)
183 # 2345->0000 (delete)
184 if LC_ALL=C expr "$oldrev" : '0*$' >/dev/null
185 then
186 change_type="create"
187 else
188 if LC_ALL=C expr "$newrev" : '0*$' >/dev/null
189 then
190 change_type="delete"
191 else
192 change_type="update"
196 # --- Get the revision types
197 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
198 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
199 case "$change_type" in
200 create|update)
201 rev="$newrev"
202 rev_type="$newrev_type"
204 delete)
205 rev="$oldrev"
206 rev_type="$oldrev_type"
208 esac
210 # The revision type tells us what type the commit is, combined with
211 # the location of the ref we can decide between
212 # - working branch
213 # - tracking branch
214 # - unannoted tag
215 # - annotated tag
216 case "$refname","$rev_type" in
217 refs/tags/*,commit)
218 # un-annotated tag
219 refname_type="tag"
220 short_refname=${refname##refs/tags/}
222 refs/tags/*,tag)
223 # annotated tag
224 refname_type="annotated tag"
225 short_refname=${refname##refs/tags/}
226 # change recipients
227 if [ -n "$announcerecipients" ]; then
228 recipients="$announcerecipients"
231 refs/heads/*,commit|refs/heads/*,tag)
232 # branch
233 refname_type="branch"
234 short_refname=${refname##refs/heads/}
236 refs/remotes/*,commit)
237 # tracking branch
238 refname_type="tracking branch"
239 short_refname=${refname##refs/remotes/}
240 echol >&2 "*** Push-update of tracking branch, $refname"
241 echol >&2 "*** - no email generated."
242 return 1
244 refs/mob/*,commit|refs/mob/*,tag)
245 # personal mob ref
246 refname_type="personal mob ref"
247 short_refname=${refname##refs/mob/}
248 echol >&2 "*** Push-update of personal mob ref, $refname"
249 echol >&2 "*** - no email generated."
250 return 1
253 # Anything else (is there anything else?)
254 echol >&2 "*** Unknown type of update to $refname ($rev_type)"
255 echol >&2 "*** - no email generated"
256 return 1
258 esac
260 # Check if we've got anyone to send to
261 if [ -z "$recipients" ]; then
262 case "$refname_type" in
263 "annotated tag")
264 config_name="hooks.announcelist"
267 config_name="hooks.mailinglist"
269 esac
270 echol >&2 "*** $config_name is not set so no email will be sent"
271 echol >&2 "*** for $refname update $oldrev->$newrev"
272 return 1
275 return 0
279 # Top level email generation function. This calls the appropriate
280 # body-generation routine after outputting the common header.
282 # Note this function doesn't actually generate any email output, that is
283 # taken care of by the functions it calls:
284 # - generate_email_header
285 # - generate_create_XXXX_email
286 # - generate_update_XXXX_email
287 # - generate_delete_XXXX_email
288 # - generate_email_footer
290 # Note also that this function cannot 'exit' from the script; when this
291 # function is running (in hook script mode), the send_mail() function
292 # is already executing in another process, connected via a pipe, and
293 # if this function exits without, whatever has been generated to that
294 # point will be sent as an email... even if nothing has been generated.
296 generate_email()
298 # Email parameters
299 # The email subject will contain the best description of the ref
300 # that we can build from the parameters
301 describe=$(git describe $rev 2>/dev/null)
302 if [ -z "$describe" ]; then
303 describe=$rev
306 generate_email_header
308 # Call the correct body generation function
309 fn_name=general
310 case "$refname_type" in
311 "tracking branch"|branch)
312 fn_name=branch
314 "annotated tag")
315 fn_name=atag
317 esac
318 generate_${change_type}_${fn_name}_email
320 generate_email_footer
323 generate_email_header()
325 # --- Email (all stdout will be the email)
326 # Generate header
327 if [ -n "$emailsender" ]; then
328 echol "From: $emailsender"
330 cat <<-EOF
331 To: $recipients
332 Subject: ${emailprefix}$projectname $refname_type $short_refname ${change_type}d: $describe
333 MIME-Version: 1.0
334 Content-Type: text/plain; charset=utf-8
335 Content-Transfer-Encoding: 8bit
337 [ -n "$cfg_suppress_x_girocco" ] || echol "X-Girocco: $cfg_gitweburl"
338 [ -z "$emailextraheader" ] || echol "$emailextraheader"
339 cat <<-EOF
340 X-Git-Refname: $refname
341 X-Git-Reftype: $refname_type
342 X-Git-Oldrev: $oldrev
343 X-Git-Newrev: $newrev
344 Auto-Submitted: auto-generated
346 This is an automated email generated because a ref change occurred in the
347 git repository for project $projectname.
349 The $refname_type, $short_refname has been ${change_type}d
353 generate_email_footer()
355 SPACE=" "
356 cat <<-EOF
359 $cfg_name automatic notification. Contact project admin $projectowner
360 if you want to unsubscribe, or site admin $cfg_admin if you receive
361 no reply.
362 --${SPACE}
363 $projectboth
367 # --------------- Branches
370 # Called for the creation of a branch
372 generate_create_branch_email()
374 # This is a new branch and so oldrev is not valid
375 echol " at $newrev ($newrev_type)"
376 echol ""
378 echol "Those revisions in this branch that are new to this repository"
379 echol "have not appeared on any other notification email; so we list"
380 echol "those revisions in full, below."
382 echol ""
383 echol $LOGBEGIN
384 show_new_revisions
385 echol $LOGEND
387 # If any revisions were shown by show_new_revisions then we show
388 # a diffstat from the last shown revisions's parent (or the empty tree
389 # if the last revision shown is a root revision) to the new revision
390 # provided the last shown revision is not a merge commit and there
391 # was at most a single boundary commit encountered by show_new_revisions.
392 # This is to show the truth of what happened in this change.
394 if [ -n "$LAST_SHOWN_REVISION" ] && [ -n "$LAST_SHOWN_NPARENTS" ] && [ -n "$LAST_SHOWN_NBOUNDARY" ] && \
395 [ "$LAST_SHOWN_NPARENTS" -le 1 ] && [ "$LAST_SHOWN_NBOUNDARY" -le 1 ]; then
396 if [ "$LAST_SHOWN_NPARENTS" -eq 0 ]; then
397 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
398 # whether or not it's actually present in the repository.
400 # Set oldrev to the result of $(git hash-object -t tree --stdin < /dev/null)
401 oldrev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
402 else
403 # Set oldrev to the parent of the last shown revision
404 oldrev="$LAST_SHOWN_REVISION^"
406 echol ""
407 echol "Summary of changes:"
408 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev $newrev
413 # Called for the change of a pre-existing branch
415 generate_update_branch_email()
417 # Consider this:
418 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
420 # O is $oldrev for $refname
421 # N is $newrev for $refname
422 # X is a revision pointed to by some other ref, for which we may
423 # assume that an email has already been generated.
424 # In this case we want to issue an email containing only revisions
425 # 3, 4, and N. Given (almost) by
427 # git rev-list N ^O --not --all
429 # The reason for the "almost", is that the "--not --all" will take
430 # precedence over the "N", and effectively will translate to
432 # git rev-list N ^O ^X ^N
434 # So, we need to build up the list more carefully. git rev-parse
435 # will generate a list of revs that may be fed into git rev-list.
436 # We can get it to make the "--not --all" part and then filter out
437 # the "^N" with:
439 # git rev-parse --not --all | grep -v N
441 # Then, using the --stdin switch to git rev-list we have effectively
442 # manufactured
444 # git rev-list N ^O ^X
446 # This leaves a problem when someone else updates the repository
447 # while this script is running. Their new value of the ref we're
448 # working on would be included in the "--not --all" output; and as
449 # our $newrev would be an ancestor of that commit, it would exclude
450 # all of our commits. What we really want is to exclude the current
451 # value of $refname from the --not list, rather than N itself. So:
453 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
455 # Get's us to something pretty safe (apart from the small time
456 # between refname being read, and git rev-parse running - for that,
457 # I give up)
460 # Next problem, consider this:
461 # * --- B --- * --- O ($oldrev)
463 # * --- X --- * --- N ($newrev)
465 # That is to say, there is no guarantee that oldrev is a strict
466 # subset of newrev (it would have required a --force, but that's
467 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
468 # Instead we find the common base of the two revs and list from
469 # there.
471 # As above, we need to take into account the presence of X; if
472 # another branch is already in the repository and points at some of
473 # the revisions that we are about to output - we don't want them.
474 # The solution is as before: git rev-parse output filtered.
476 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
478 # Tags pushed into the repository generate nice shortlog emails that
479 # summarise the commits between them and the previous tag. However,
480 # those emails don't include the full commit messages that we output
481 # for a branch update. Therefore we still want to output revisions
482 # that have been output on a tag email.
484 # Luckily, git rev-parse includes just the tool. Instead of using
485 # "--all" we use "--branches"; this has the added benefit that
486 # "remotes/" will be ignored as well.
488 # List all of the revisions that were removed by this update, in a
489 # fast-forward update, this list will be empty, because rev-list O
490 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
491 # revisions
492 fast_forward=""
493 rev=""
494 for rev in $(git rev-list $newrev..$oldrev)
496 revtype=$(git cat-file -t "$rev")
497 echol " discards $rev ($revtype)"
498 done
499 if [ -z "$rev" ]; then
500 fast_forward=1
503 # List all the revisions from baserev to newrev in a kind of
504 # "table-of-contents"; note this list can include revisions that
505 # have already had notification emails and is present to show the
506 # full detail of the change from rolling back the old revision to
507 # the base revision and then forward to the new revision
508 for rev in $(git rev-list $oldrev..$newrev)
510 revtype=$(git cat-file -t "$rev")
511 echol " via $rev ($revtype)"
512 done
514 if [ "$fast_forward" ]; then
515 echol " from $oldrev ($oldrev_type)"
516 else
517 # 1. Existing revisions were removed. In this case newrev
518 # is a subset of oldrev - this is the reverse of a
519 # fast-forward, a rewind
520 # 2. New revisions were added on top of an old revision,
521 # this is a rewind and addition.
523 # (1) certainly happened, (2) possibly. When (2) hasn't
524 # happened, we set a flag to indicate that no log printout
525 # is required.
527 echol ""
529 # Find the common ancestor of the old and new revisions and
530 # compare it with newrev
531 baserev=$(git merge-base $oldrev $newrev)
532 rewind_only=""
533 if [ "$baserev" = "$newrev" ]; then
534 echol "This update discarded existing revisions and left the branch pointing at"
535 echol "a previous point in the repository history."
536 echol ""
537 echol " * -- * -- N ($newrev)"
538 echol " \\"
539 echol " O -- O -- O ($oldrev)"
540 echol ""
541 echol "The removed revisions are not necessarily gone - if another reference"
542 echol "still refers to them they will stay in the repository."
543 rewind_only=1
544 else
545 echol "This update added new revisions after undoing existing revisions. That is"
546 echol "to say, the old revision is not a strict subset of the new revision. This"
547 echol "situation occurs when you --force push a change and generate a repository"
548 echol "containing something like this:"
549 echol ""
550 echol " * -- * -- B -- O -- O -- O ($oldrev)"
551 echol " \\"
552 echol " N -- N -- N ($newrev)"
553 echol ""
554 echol "When this happens we assume that you've already had alert emails for all"
555 echol "of the O revisions, and so we here report only the revisions in the N"
556 echol "branch from the common base, B."
560 echol ""
561 if [ -z "$rewind_only" ]; then
562 echol "Those revisions listed above that are new to this repository have"
563 echol "not appeared on any other notification email; so we list those"
564 echol "revisions in full, below."
566 echol ""
567 echol $LOGBEGIN
568 show_new_revisions
570 # XXX: Need a way of detecting whether git rev-list actually
571 # outputted anything, so that we can issue a "no new
572 # revisions added by this update" message
574 echol $LOGEND
575 else
576 echol "No new revisions were added by this update."
579 # The diffstat is shown from the old revision to the new revision.
580 # This is to show the truth of what happened in this change.
581 # There's no point showing the stat from the base to the new
582 # revision because the base is effectively a random revision at this
583 # point - the user will be interested in what this revision changed
584 # - including the undoing of previous revisions in the case of
585 # non-fast-forward updates.
586 echol ""
587 echol "Summary of changes:"
588 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev..$newrev
592 # Called for the deletion of a branch
594 generate_delete_branch_email()
596 echol " was $oldrev"
597 echol ""
598 echol $LOGBEGIN
599 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
600 echol $LOGEND
603 # --------------- Annotated tags
606 # Called for the creation of an annotated tag
608 generate_create_atag_email()
610 echol " at $newrev ($newrev_type)"
612 generate_atag_email
616 # Called for the update of an annotated tag (this is probably a rare event
617 # and may not even be allowed)
619 generate_update_atag_email()
621 echol " to $newrev ($newrev_type)"
622 echol " from $oldrev (which is now obsolete)"
624 generate_atag_email
628 # Called when an annotated tag is created or changed
630 generate_atag_email()
632 # Use read_tag_fields to pull out the individual fields from the
633 # tag and git cat-file --batch-check to fully peel the tag if needed
634 tagrev="$newrev"
635 tagrev16="$newrev16"
636 read_tag_fields "$tagrev"
637 if [ "$tagtype" = "tag" ]; then
638 # fully peel the tag
639 peeledobject=
640 peeledtype=
641 info="$(echo "$tagrev^{}" | git cat-file --batch-check='
642 peeledobject=%(objectname)
643 peeledtype=%(objecttype)'
645 case "$info" in *type=*) eval "$info";; *)
646 peeledtype=missing
647 esac
648 else
649 peeledobject="$tagobject"
650 peeledtype="$tagtype"
653 echol " tagging $peeledobject ($peeledtype)"
654 case "$peeledtype" in
655 commit)
657 # If the tagged object is a commit, then we assume this is a
658 # release, and so we calculate which tag this tag is
659 # replacing
660 prevtag=$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)
662 if [ -n "$prevtag" ]; then
663 echol " replaces $prevtag"
667 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
669 esac
670 echol " tagged by $taggername"
671 echol " on $taggerdate"
673 echol ""
674 echol $LOGBEGIN
676 while
677 echol "tag $tagrev"
678 echol "Tag: $tagtag"
679 echol "Object: $tagobject ($tagtype)"
680 [ -z "$taggername" ] || \
681 echol "Tagger: $taggername"
682 [ -z "$taggerdate" ] || \
683 echol "Date: $taggerdate"
684 echol "URL: <$projurl/$tagrev16>"
685 echol ""
687 # Show the content of the tag message; this might contain a change
688 # log or release notes so is worth displaying.
689 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
690 echol ""
691 [ "$tagtype" = "tag" ]
693 tagrev="$tagobject"
694 scratch="${tagrev#????????????????}"
695 tagrev16="${tagrev%$scratch}"
696 read_tag_fields "$tagrev" || break
697 done
699 case "$peeledtype" in
700 commit)
701 # Only commit tags make sense to have rev-list operations
702 # performed on them
703 if [ -n "$prevtag" ]; then
704 # Show changes since the previous release
705 git shortlog "$prevtag..$newrev"
706 else
707 # No previous tag, show all the changes since time
708 # began
709 git shortlog $newrev
713 # XXX: Is there anything useful we can do for non-commit
714 # objects?
716 esac
718 echol $LOGEND
722 # Called for the deletion of an annotated tag
724 generate_delete_atag_email()
726 echol " was $oldrev"
727 echol ""
728 echol $LOGBEGIN
729 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
730 echol $LOGEND
733 # --------------- General references
736 # Called when any other type of reference is created (most likely a
737 # non-annotated tag)
739 generate_create_general_email()
741 echol " at $newrev ($newrev_type)"
743 generate_general_email
747 # Called when any other type of reference is updated (most likely a
748 # non-annotated tag)
750 generate_update_general_email()
752 echol " to $newrev ($newrev_type)"
753 echol " from $oldrev"
755 generate_general_email
759 # Called for creation or update of any other type of reference
761 generate_general_email()
763 # Unannotated tags are more about marking a point than releasing a
764 # version; therefore we don't do the shortlog summary that we do for
765 # annotated tags above - we simply show that the point has been
766 # marked, and print the log message for the marked point for
767 # reference purposes
769 # Note this section also catches any other reference type (although
770 # there aren't any) and deals with them in the same way.
772 echol ""
773 if [ "$newrev_type" = "commit" ]; then
774 if [ -n "$(git rev-list --no-walk --merges $newrev)" ]; then
775 pfmt12="$pfmt1$pfmt1m$pfmt2"
776 else
777 pfmt12="$pfmt1$pfmt2"
779 echol $LOGBEGIN
780 git diff-tree --no-color --date=$datefmt --root -s --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$newrev16$pfmt3" --abbrev=$habbr $newrev
781 echol $LOGEND
782 else
783 # What can we do here? The tag marks an object that is not
784 # a commit, so there is no log for us to display. It's
785 # probably not wise to output git cat-file as it could be a
786 # binary blob. We'll just say how big it is
787 echol "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
792 # Called for the deletion of any other type of reference
794 generate_delete_general_email()
796 echol " was $oldrev"
797 echol ""
798 echol $LOGBEGIN
799 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
800 echol $LOGEND
804 # --------------- Miscellaneous utilities
807 # Show new revisions as the user would like to see them in the email.
809 # On return LAST_SHOWN_REVISION will be set to non-empty if any revisions shown
810 # and LAST_SHOWN_NPARENTS will be the number of parents it has (possibly 0)
811 # and LAST_SHOWN_NBOUNDARY will be the number of shown leaves (possibly 0)
813 show_new_revisions()
815 # This shows all log entries that are not already covered by
816 # another ref - i.e. commits that are now accessible from this
817 # ref that were previously not accessible
818 # (see generate_update_branch_email for the explanation of this
819 # command)
820 LAST_SHOWN_REVISION=
821 LAST_SHOWN_NPARENTS=
822 LAST_SHOWN_NBOUNDARY=0
824 # Revision range passed to rev-list differs for new vs. updated
825 # branches.
826 if [ "$change_type" = create ]
827 then
828 # Show all revisions exclusive to this (new) branch.
829 revspec=$newrev
830 else
831 # Branch update; show revisions not part of $oldrev.
832 revspec=$oldrev..$newrev
835 if [ "${MAIL_SH_OTHER_BRANCHES+set}" = "set" ]; then
836 case "$MAIL_SH_OTHER_BRANCHES" in
838 othertips="git cat-file blob '${MAIL_SH_OTHER_BRANCHES#@}' 2>/dev/null"
841 othertips='printf "%s\n" $MAIL_SH_OTHER_BRANCHES'
843 esac
844 else
845 othertips='git for-each-ref --format="%(refname)" refs/heads |
846 LC_ALL=C awk -v "refname=$refname" "\$1 != refname"'
848 # if [ -z "$custom_showrev" ]
849 # then
850 # git rev-list --pretty --stdin $revspec
851 # else
852 while read onerev mark pcnt && [ -n "$onerev" ] && [ -n "$mark" ] && [ -n "$pcnt" ]
854 if [ "$mark" = "-" ]; then
855 LAST_SHOWN_NBOUNDARY=$(( $LAST_SHOWN_NBOUNDARY + 1 ))
856 continue
858 LAST_SHOWN_REVISION="$onerev"
859 LAST_SHOWN_NPARENTS="$pcnt"
860 if [ -n "$custom_showrev" ]; then
861 eval $(printf "$custom_showrev" $onerev)
862 else
863 if [ ${summaryonly:-false} = false ]; then
864 if [ ${pcnt:-1} -gt 1 ]; then
865 opts="-p --cc"
866 pfmt12="$pfmt1$pfmt1m$pfmt2"
867 else
868 opts="-p --stat=72 --summary"
869 pfmt12="$pfmt1$pfmt2"
871 else
872 if [ ${pcnt:-1} -gt 1 ]; then
873 opts="-s"
874 pfmt12="$pfmt1$pfmt1m$pfmt2"
875 else
876 opts="--stat=72 --summary"
877 pfmt12="$pfmt1$pfmt2"
880 scratch="${onerev#????????????????}"
881 onerev16="${onerev%$scratch}"
882 git diff-tree --no-color --date=$datefmt $opts --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$onerev16$pfmt3" --abbrev=$habbr -B -C --root $onerev
883 echo
885 done <<EOT
886 $(eval "$othertips" |
887 git cat-file --batch-check='^%(objectname)' |
888 LC_ALL=C sed -e '/ missing$/d' |
889 git --no-pager log --stdin --no-color --boundary --format=tformat:"%H %m %p" $revspec |
890 LC_ALL=C awk '{print $1 " " $2 " " NF-2}')
892 # fi
896 size_limit()
898 size=0
899 while IFS= read -r line; do
900 # Include size of RFC 822 trailing CR+LF on each line
901 size=$(($size+${#line}+2))
902 if [ $size -gt $1 ]; then
903 echol "...e-mail trimmed, has been too large."
904 break
906 echol "$line"
907 done
911 sendmail_to_stdout()
917 send_mail()
919 if [ -n "$cfg_sender" ]; then
920 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
921 else
922 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
926 # ---------------------------- main()
928 # --- Constants
929 LOGBEGIN="- Log -----------------------------------------------------------------"
930 LOGEND="-----------------------------------------------------------------------"
932 # --- Config
933 . @basedir@/shlib.sh
935 # Git formatting to use
936 datefmt=rfc2822
937 habbr=12
939 # strftime formatting to use (empty is default rfc2822 format)
940 sdatefmt=
942 # This is --pretty=medium in four parts with a URL: line added
943 # The pfmt1m value must be inserted after the pfmt1 value but only for merges
944 # The URL must be inserted between pfmt2 and pfmt3
945 pfmt1='format:commit %H%n'
946 pfmt1m='Merge: %p%n'
947 pfmt2='Author: %an <%ae>%nDate: %ad%nURL: <'
948 pfmt3='>%n%n%w(0,4,4)%s%n%n%b'
950 git_add_config core.abbrev=$habbr
952 # Set GIT_DIR either from the working directory, or from the environment
953 # variable.
954 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
955 if [ -z "$GIT_DIR" ]; then
956 echol >&2 "fatal: mail.sh: GIT_DIR not set"
957 exit 1
960 projectdesc=
961 ! [ -s "$GIT_DIR/description" ] || projectdesc=$(LC_ALL=C sed -ne '1p' "$GIT_DIR/description")
962 # Check if the description is unchanged from it's default, and shorten it to
963 # a more manageable length if it is
964 if [ -z "$projectdesc" ] || LC_ALL=C expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
965 then
966 projectdesc="UNNAMED PROJECT"
969 # If --stdout is first argument send all output there instead of mailing
970 if [ "$1" = "--stdout" ]; then
971 shift
972 cfg_sendmail_bin="sendmail_to_stdout"
975 projectname="${4%.git}"
976 if [ -n "$projectname" ]; then
977 projectname="$projectname.git"
978 projectowner="$(config_get owner)"
979 else
980 projectname="$(basename "$PWD")"
982 projectboth="$projectname (\"$projectdesc\")"
983 projurl="$cfg_gitweburl/$projectname"
984 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
986 emailsender="$5"
987 emailextraheader="$6"
989 recipients=$(git config hooks.mailinglist)
990 summaryonly=$(git config --bool hooks.summaryonly 2>/dev/null || :)
991 announcerecipients=$(git config hooks.announcelist)
992 envelopesender=$(git config hooks.envelopesender)
993 emailprefix=$(git config hooks.emailprefix || echol "[$cfg_name] ")
994 custom_showrev=$(git config hooks.showrev)
996 # --- Main loop
997 # Allow dual mode: run from the command line just like the update hook, or
998 # if no arguments are given then run as a hook script
999 # If --stdout is first argument send all output there instead (handled above)
1000 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
1001 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
1002 # Handle a single update rather than a batch on stdin
1003 # Output will still be sent to sendmail unless --stdout is used
1004 # Same 3 args as update hook (<refname> <old> <new>)
1005 if prep_for_email $2 $3 $1; then
1006 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1008 else
1009 # MAIL_SH_OTHER_BRANCHES cannot possibly be valid for multiple updates
1010 unset MAIL_SH_OTHER_BRANCHES
1011 # Same input as pre-receive hook (each line is <old> <new> <refname>)
1012 while read oldrev newrev refname
1014 prep_for_email $oldrev $newrev $refname || continue
1015 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1016 done
1019 exit 0