gc.sh: correct crud pattern .tmp-?*-pack to .tmp-?*-pack*
[girocco.git] / taskd / mail.sh
blob4b8dae67d20c7897b51c15a77285e1cc89f0aa65
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 [ "$oldrev" != "$newrev" ] || return 1
178 scratch="${newrev#????????????????}"
179 newrev16="${newrev%$scratch}"
180 refname="$3"
182 # --- Interpret
183 # 0000->1234 (create)
184 # 1234->2345 (update)
185 # 2345->0000 (delete)
186 if LC_ALL=C expr "$oldrev" : '0*$' >/dev/null
187 then
188 change_type="create"
189 else
190 if LC_ALL=C expr "$newrev" : '0*$' >/dev/null
191 then
192 change_type="delete"
193 else
194 change_type="update"
198 # --- Get the revision types
199 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
200 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
201 case "$change_type" in
202 create|update)
203 rev="$newrev"
204 rev_type="$newrev_type"
206 delete)
207 rev="$oldrev"
208 rev_type="$oldrev_type"
210 esac
212 # The revision type tells us what type the commit is, combined with
213 # the location of the ref we can decide between
214 # - working branch
215 # - tracking branch
216 # - unannoted tag
217 # - annotated tag
218 case "$refname","$rev_type" in
219 refs/tags/*,commit)
220 # un-annotated tag
221 refname_type="tag"
222 short_refname=${refname##refs/tags/}
224 refs/tags/*,tag)
225 # annotated tag
226 refname_type="annotated tag"
227 short_refname=${refname##refs/tags/}
228 # change recipients
229 if [ -n "$announcerecipients" ]; then
230 recipients="$announcerecipients"
233 refs/heads/*,commit|refs/heads/*,tag)
234 # branch
235 refname_type="branch"
236 short_refname=${refname##refs/heads/}
238 refs/remotes/*,commit)
239 # tracking branch
240 refname_type="tracking branch"
241 short_refname=${refname##refs/remotes/}
242 echol >&2 "*** Push-update of tracking branch, $refname"
243 echol >&2 "*** - no email generated."
244 return 1
246 refs/mob/*,commit|refs/mob/*,tag)
247 # personal mob ref
248 refname_type="personal mob ref"
249 short_refname=${refname##refs/mob/}
250 echol >&2 "*** Push-update of personal mob ref, $refname"
251 echol >&2 "*** - no email generated."
252 return 1
255 # Anything else (is there anything else?)
256 echol >&2 "*** Unknown type of update to $refname ($rev_type)"
257 echol >&2 "*** - no email generated"
258 return 1
260 esac
262 # Check if we've got anyone to send to
263 if [ -z "$recipients" ]; then
264 case "$refname_type" in
265 "annotated tag")
266 config_name="hooks.announcelist"
269 config_name="hooks.mailinglist"
271 esac
272 echol >&2 "*** $config_name is not set so no email will be sent"
273 echol >&2 "*** for $refname update $oldrev->$newrev"
274 return 1
277 return 0
281 # Top level email generation function. This calls the appropriate
282 # body-generation routine after outputting the common header.
284 # Note this function doesn't actually generate any email output, that is
285 # taken care of by the functions it calls:
286 # - generate_email_header
287 # - generate_create_XXXX_email
288 # - generate_update_XXXX_email
289 # - generate_delete_XXXX_email
290 # - generate_email_footer
292 # Note also that this function cannot 'exit' from the script; when this
293 # function is running (in hook script mode), the send_mail() function
294 # is already executing in another process, connected via a pipe, and
295 # if this function exits without, whatever has been generated to that
296 # point will be sent as an email... even if nothing has been generated.
298 generate_email()
300 # Email parameters
301 # The email subject will contain the best description of the ref
302 # that we can build from the parameters
303 describe=$(git describe $rev 2>/dev/null)
304 if [ -z "$describe" ]; then
305 describe=$rev
308 generate_email_header
310 # Call the correct body generation function
311 fn_name=general
312 case "$refname_type" in
313 "tracking branch"|branch)
314 fn_name=branch
316 "annotated tag")
317 fn_name=atag
319 esac
320 generate_${change_type}_${fn_name}_email
322 generate_email_footer
325 generate_email_header()
327 # --- Email (all stdout will be the email)
328 # Generate header
329 if [ -n "$emailsender" ]; then
330 echol "From: $emailsender"
332 cat <<-EOF
333 To: $recipients
334 Subject: ${emailprefix}$projectname $refname_type $short_refname ${change_type}d: $describe
335 MIME-Version: 1.0
336 Content-Type: text/plain; charset=utf-8
337 Content-Transfer-Encoding: 8bit
339 [ -n "$cfg_suppress_x_girocco" ] || echol "X-Girocco: $cfg_gitweburl"
340 [ -z "$emailextraheader" ] || echol "$emailextraheader"
341 cat <<-EOF
342 X-Git-Refname: $refname
343 X-Git-Reftype: $refname_type
344 X-Git-Oldrev: $oldrev
345 X-Git-Newrev: $newrev
346 Auto-Submitted: auto-generated
348 This is an automated email generated because a ref change occurred in the
349 git repository for project $projectname.
351 The $refname_type, $short_refname has been ${change_type}d
355 generate_email_footer()
357 SPACE=" "
358 cat <<-EOF
361 $cfg_name automatic notification. Contact project admin $projectowner
362 if you want to unsubscribe, or site admin $cfg_admin if you receive
363 no reply.
364 --${SPACE}
365 $projectboth
369 # --------------- Branches
372 # Called for the creation of a branch
374 generate_create_branch_email()
376 # This is a new branch and so oldrev is not valid
377 echol " at $newrev ($newrev_type)"
378 echol ""
380 echol "Those revisions in this branch that are new to this repository"
381 echol "have not appeared on any other notification email; so we list"
382 echol "those revisions in full, below."
384 echol ""
385 echol $LOGBEGIN
386 show_new_revisions
387 echol $LOGEND
389 # If any revisions were shown by show_new_revisions then we show
390 # a diffstat from the last shown revisions's parent (or the empty tree
391 # if the last revision shown is a root revision) to the new revision
392 # provided the last shown revision is not a merge commit and there
393 # were no more boundary commits encountered by show_new_revisions than
394 # the last shown revision has parents.
395 # This is to show the truth of what happened in this change.
397 if [ -n "$LAST_SHOWN_REVISION" ] && [ -n "$LAST_SHOWN_NPARENTS" ] && [ -n "$LAST_SHOWN_NBOUNDARY" ] && \
398 [ "$LAST_SHOWN_NPARENTS" -le 1 ] && [ "$LAST_SHOWN_NBOUNDARY" -le "$LAST_SHOWN_NPARENTS" ]; then
399 if [ "$LAST_SHOWN_NPARENTS" -eq 0 ]; then
400 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
401 # whether or not it's actually present in the repository.
403 # Set oldrev to the result of $(git hash-object -t tree --stdin < /dev/null)
404 oldrev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
405 else
406 # Set oldrev to the parent of the last shown revision
407 oldrev="$LAST_SHOWN_REVISION^"
409 echol ""
410 echol "Summary of changes:"
411 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev $newrev
416 # Called for the change of a pre-existing branch
418 generate_update_branch_email()
420 # Consider this:
421 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
423 # O is $oldrev for $refname
424 # N is $newrev for $refname
425 # X is a revision pointed to by some other ref, for which we may
426 # assume that an email has already been generated.
427 # In this case we want to issue an email containing only revisions
428 # 3, 4, and N. Given (almost) by
430 # git rev-list N ^O --not --all
432 # The reason for the "almost", is that the "--not --all" will take
433 # precedence over the "N", and effectively will translate to
435 # git rev-list N ^O ^X ^N
437 # So, we need to build up the list more carefully. git rev-parse
438 # will generate a list of revs that may be fed into git rev-list.
439 # We can get it to make the "--not --all" part and then filter out
440 # the "^N" with:
442 # git rev-parse --not --all | grep -v N
444 # Then, using the --stdin switch to git rev-list we have effectively
445 # manufactured
447 # git rev-list N ^O ^X
449 # This leaves a problem when someone else updates the repository
450 # while this script is running. Their new value of the ref we're
451 # working on would be included in the "--not --all" output; and as
452 # our $newrev would be an ancestor of that commit, it would exclude
453 # all of our commits. What we really want is to exclude the current
454 # value of $refname from the --not list, rather than N itself. So:
456 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
458 # Get's us to something pretty safe (apart from the small time
459 # between refname being read, and git rev-parse running - for that,
460 # I give up)
463 # Next problem, consider this:
464 # * --- B --- * --- O ($oldrev)
466 # * --- X --- * --- N ($newrev)
468 # That is to say, there is no guarantee that oldrev is a strict
469 # subset of newrev (it would have required a --force, but that's
470 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
471 # Instead we find the common base of the two revs and list from
472 # there.
474 # As above, we need to take into account the presence of X; if
475 # another branch is already in the repository and points at some of
476 # the revisions that we are about to output - we don't want them.
477 # The solution is as before: git rev-parse output filtered.
479 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
481 # Tags pushed into the repository generate nice shortlog emails that
482 # summarise the commits between them and the previous tag. However,
483 # those emails don't include the full commit messages that we output
484 # for a branch update. Therefore we still want to output revisions
485 # that have been output on a tag email.
487 # Luckily, git rev-parse includes just the tool. Instead of using
488 # "--all" we use "--branches"; this has the added benefit that
489 # "remotes/" will be ignored as well.
491 # List all of the revisions that were removed by this update, in a
492 # fast-forward update, this list will be empty, because rev-list O
493 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
494 # revisions
495 fast_forward=""
496 rev=""
497 for rev in $(git rev-list $newrev..$oldrev)
499 revtype=$(git cat-file -t "$rev")
500 echol " discards $rev ($revtype)"
501 done
502 if [ -z "$rev" ]; then
503 fast_forward=1
506 # List all the revisions from baserev to newrev in a kind of
507 # "table-of-contents"; note this list can include revisions that
508 # have already had notification emails and is present to show the
509 # full detail of the change from rolling back the old revision to
510 # the base revision and then forward to the new revision
511 for rev in $(git rev-list $oldrev..$newrev)
513 revtype=$(git cat-file -t "$rev")
514 echol " via $rev ($revtype)"
515 done
517 if [ "$fast_forward" ]; then
518 echol " from $oldrev ($oldrev_type)"
519 else
520 # 1. Existing revisions were removed. In this case newrev
521 # is a subset of oldrev - this is the reverse of a
522 # fast-forward, a rewind
523 # 2. New revisions were added on top of an old revision,
524 # this is a rewind and addition.
526 # (1) certainly happened, (2) possibly. When (2) hasn't
527 # happened, we set a flag to indicate that no log printout
528 # is required.
530 echol ""
532 # Find the common ancestor of the old and new revisions and
533 # compare it with newrev
534 baserev=$(git merge-base $oldrev $newrev)
535 rewind_only=""
536 if [ "$baserev" = "$newrev" ]; then
537 echol "This update discarded existing revisions and left the branch pointing at"
538 echol "a previous point in the repository history."
539 echol ""
540 echol " * -- * -- N ($newrev)"
541 echol " \\"
542 echol " O -- O -- O ($oldrev)"
543 echol ""
544 echol "The removed revisions are not necessarily gone - if another reference"
545 echol "still refers to them they will stay in the repository."
546 rewind_only=1
547 else
548 echol "This update added new revisions after undoing existing revisions. That is"
549 echol "to say, the old revision is not a strict subset of the new revision. This"
550 echol "situation occurs when you --force push a change and generate a repository"
551 echol "containing something like this:"
552 echol ""
553 echol " * -- * -- B -- O -- O -- O ($oldrev)"
554 echol " \\"
555 echol " N -- N -- N ($newrev)"
556 echol ""
557 echol "When this happens we assume that you've already had alert emails for all"
558 echol "of the O revisions, and so we here report only the revisions in the N"
559 echol "branch from the common base, B."
563 echol ""
564 if [ -z "$rewind_only" ]; then
565 echol "Those revisions listed above that are new to this repository have"
566 echol "not appeared on any other notification email; so we list those"
567 echol "revisions in full, below."
569 echol ""
570 echol $LOGBEGIN
571 show_new_revisions
573 # XXX: Need a way of detecting whether git rev-list actually
574 # outputted anything, so that we can issue a "no new
575 # revisions added by this update" message
577 echol $LOGEND
578 else
579 echol "No new revisions were added by this update."
582 # The diffstat is shown from the old revision to the new revision.
583 # This is to show the truth of what happened in this change.
584 # There's no point showing the stat from the base to the new
585 # revision because the base is effectively a random revision at this
586 # point - the user will be interested in what this revision changed
587 # - including the undoing of previous revisions in the case of
588 # non-fast-forward updates.
589 echol ""
590 echol "Summary of changes:"
591 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev..$newrev
595 # Called for the deletion of a branch
597 generate_delete_branch_email()
599 echol " was $oldrev"
600 echol ""
601 echol $LOGBEGIN
602 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
603 echol $LOGEND
606 # --------------- Annotated tags
609 # Called for the creation of an annotated tag
611 generate_create_atag_email()
613 echol " at $newrev ($newrev_type)"
615 generate_atag_email
619 # Called for the update of an annotated tag (this is probably a rare event
620 # and may not even be allowed)
622 generate_update_atag_email()
624 echol " to $newrev ($newrev_type)"
625 echol " from $oldrev (which is now obsolete)"
627 generate_atag_email
631 # Called when an annotated tag is created or changed
633 generate_atag_email()
635 # Use read_tag_fields to pull out the individual fields from the
636 # tag and git cat-file --batch-check to fully peel the tag if needed
637 tagrev="$newrev"
638 tagrev16="$newrev16"
639 read_tag_fields "$tagrev"
640 if [ "$tagtype" = "tag" ]; then
641 # fully peel the tag
642 peeledobject=
643 peeledtype=
644 info="$(echo "$tagrev^{}" | git cat-file --batch-check='
645 peeledobject=%(objectname)
646 peeledtype=%(objecttype)'
648 case "$info" in *type=*) eval "$info";; *)
649 peeledtype=missing
650 esac
651 else
652 peeledobject="$tagobject"
653 peeledtype="$tagtype"
656 echol " tagging $peeledobject ($peeledtype)"
657 case "$peeledtype" in
658 commit)
660 # If the tagged object is a commit, then we assume this is a
661 # release, and so we calculate which tag this tag is
662 # replacing
663 prevtag=$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)
665 if [ -n "$prevtag" ]; then
666 echol " replaces $prevtag"
670 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
672 esac
673 echol " tagged by $taggername"
674 echol " on $taggerdate"
676 echol ""
677 echol $LOGBEGIN
679 while
680 echol "tag $tagrev"
681 echol "Tag: $tagtag"
682 echol "Object: $tagobject ($tagtype)"
683 [ -z "$taggername" ] || \
684 echol "Tagger: $taggername"
685 [ -z "$taggerdate" ] || \
686 echol "Date: $taggerdate"
687 echol "URL: <$projurl/$tagrev16>"
688 echol ""
690 # Show the content of the tag message; this might contain a change
691 # log or release notes so is worth displaying.
692 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
693 echol ""
694 [ "$tagtype" = "tag" ]
696 tagrev="$tagobject"
697 scratch="${tagrev#????????????????}"
698 tagrev16="${tagrev%$scratch}"
699 read_tag_fields "$tagrev" || break
700 done
702 case "$peeledtype" in
703 commit)
704 # Only commit tags make sense to have rev-list operations
705 # performed on them
706 if [ -n "$prevtag" ]; then
707 # Show changes since the previous release
708 git shortlog "$prevtag..$newrev"
709 else
710 # No previous tag, show all the changes since time
711 # began
712 git shortlog $newrev
716 # XXX: Is there anything useful we can do for non-commit
717 # objects?
719 esac
721 echol $LOGEND
725 # Called for the deletion of an annotated tag
727 generate_delete_atag_email()
729 echol " was $oldrev"
730 echol ""
731 echol $LOGBEGIN
732 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
733 echol $LOGEND
736 # --------------- General references
739 # Called when any other type of reference is created (most likely a
740 # non-annotated tag)
742 generate_create_general_email()
744 echol " at $newrev ($newrev_type)"
746 generate_general_email
750 # Called when any other type of reference is updated (most likely a
751 # non-annotated tag)
753 generate_update_general_email()
755 echol " to $newrev ($newrev_type)"
756 echol " from $oldrev"
758 generate_general_email
762 # Called for creation or update of any other type of reference
764 generate_general_email()
766 # Unannotated tags are more about marking a point than releasing a
767 # version; therefore we don't do the shortlog summary that we do for
768 # annotated tags above - we simply show that the point has been
769 # marked, and print the log message for the marked point for
770 # reference purposes
772 # Note this section also catches any other reference type (although
773 # there aren't any) and deals with them in the same way.
775 echol ""
776 if [ "$newrev_type" = "commit" ]; then
777 if [ -n "$(git rev-list --no-walk --merges $newrev)" ]; then
778 pfmt12="$pfmt1$pfmt1m$pfmt2"
779 else
780 pfmt12="$pfmt1$pfmt2"
782 echol $LOGBEGIN
783 git diff-tree --no-color --date=$datefmt --root -s --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$newrev16$pfmt3" --abbrev=$habbr $newrev
784 echol $LOGEND
785 else
786 # What can we do here? The tag marks an object that is not
787 # a commit, so there is no log for us to display. It's
788 # probably not wise to output git cat-file as it could be a
789 # binary blob. We'll just say how big it is
790 echol "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
795 # Called for the deletion of any other type of reference
797 generate_delete_general_email()
799 echol " was $oldrev"
800 echol ""
801 echol $LOGBEGIN
802 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
803 echol $LOGEND
807 # --------------- Miscellaneous utilities
810 # Show new revisions as the user would like to see them in the email.
812 # On return LAST_SHOWN_REVISION will be set to non-empty if any revisions shown
813 # and LAST_SHOWN_NPARENTS will be the number of parents it has (possibly 0)
814 # and LAST_SHOWN_NBOUNDARY will be the total number of boundary commits seen (possibly 0)
815 # So if $LAST_SHOWN_NBOUNDARY is greater than $LAST_SHOWN_NPARENTS then the
816 # portion of the graph shown by show_new_revisions (excluding $LAST_SHOWN_REVISION)
817 # includes at least one merge commit that had at least one parent excluded.
819 show_new_revisions()
821 # This shows all log entries that are not already covered by
822 # another ref - i.e. commits that are now accessible from this
823 # ref that were previously not accessible
824 # (see generate_update_branch_email for the explanation of this
825 # command)
826 LAST_SHOWN_REVISION=
827 LAST_SHOWN_NPARENTS=
828 LAST_SHOWN_NBOUNDARY=0
830 # Revision range passed to rev-list differs for new vs. updated
831 # branches.
832 if [ "$change_type" = create ]
833 then
834 # Show all revisions exclusive to this (new) branch.
835 revspec=$newrev
836 else
837 # Branch update; show revisions not part of $oldrev.
838 revspec=$oldrev..$newrev
841 if [ "${MAIL_SH_OTHER_BRANCHES+set}" = "set" ]; then
842 case "$MAIL_SH_OTHER_BRANCHES" in
844 othertips="git cat-file blob '${MAIL_SH_OTHER_BRANCHES#@}' 2>/dev/null"
847 othertips='printf "%s\n" $MAIL_SH_OTHER_BRANCHES'
849 esac
850 else
851 othertips='git for-each-ref --format="%(refname)" refs/heads |
852 LC_ALL=C awk -v "refname=$refname" "\$1 != refname"'
854 # if [ -z "$custom_showrev" ]
855 # then
856 # git rev-list --pretty --stdin $revspec
857 # else
858 while read onerev mark pcnt && [ -n "$onerev" ] && [ -n "$mark" ] && [ -n "$pcnt" ]
860 if [ "$mark" = "-" ]; then
861 LAST_SHOWN_NBOUNDARY=$(( $LAST_SHOWN_NBOUNDARY + 1 ))
862 continue
864 if [ -z "$reverseopt" ] || [ -z "$LAST_SHOWN_REVISION" ]; then
865 LAST_SHOWN_REVISION="$onerev"
866 LAST_SHOWN_NPARENTS="$pcnt"
868 if [ -n "$custom_showrev" ]; then
869 eval $(printf "$custom_showrev" $onerev)
870 else
871 if [ ${summaryonly:-false} = false ]; then
872 if [ ${pcnt:-1} -gt 1 ]; then
873 opts="-p --cc"
874 pfmt12="$pfmt1$pfmt1m$pfmt2"
875 else
876 opts="-p --stat=72 --summary"
877 pfmt12="$pfmt1$pfmt2"
879 else
880 if [ ${pcnt:-1} -gt 1 ]; then
881 opts="-s"
882 pfmt12="$pfmt1$pfmt1m$pfmt2"
883 else
884 opts="--stat=72 --summary"
885 pfmt12="$pfmt1$pfmt2"
888 scratch="${onerev#????????????????}"
889 onerev16="${onerev%$scratch}"
890 git diff-tree --no-color --date=$datefmt $opts --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$onerev16$pfmt3" --abbrev=$habbr -B -C --root $onerev
891 echo
893 done <<EOT
894 $(eval "$othertips" |
895 git cat-file --batch-check='^%(objectname)' |
896 LC_ALL=C sed -e '/ missing$/d' |
897 git --no-pager log --stdin --no-color $reverseopt --boundary --format=tformat:"%H %m %p" $revspec |
898 LC_ALL=C awk '{print $1 " " $2 " " NF-2}')
900 # fi
904 size_limit()
906 size=0
907 while IFS= read -r line; do
908 # Include size of RFC 822 trailing CR+LF on each line
909 size=$(($size+${#line}+2))
910 if [ $size -gt $1 ]; then
911 echol "...e-mail trimmed, has been too large."
912 break
914 echol "$line"
915 done
919 sendmail_to_stdout()
925 send_mail()
927 if [ -n "$cfg_sender" ]; then
928 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
929 else
930 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
934 # ---------------------------- main()
936 # --- Constants
937 LOGBEGIN="- Log -----------------------------------------------------------------"
938 LOGEND="-----------------------------------------------------------------------"
940 # --- Config
941 . @basedir@/shlib.sh
943 # Git formatting to use
944 datefmt=rfc2822
945 habbr=12
947 # strftime formatting to use (empty is default rfc2822 format)
948 sdatefmt=
950 # This is --pretty=medium in four parts with a URL: line added
951 # The pfmt1m value must be inserted after the pfmt1 value but only for merges
952 # The URL must be inserted between pfmt2 and pfmt3
953 pfmt1='format:commit %H%n'
954 pfmt1m='Merge: %p%n'
955 pfmt2='Author: %an <%ae>%nDate: %ad%nURL: <'
956 pfmt3='>%n%n%w(0,4,4)%s%n%n%b'
958 git_add_config core.abbrev=$habbr
960 # Set GIT_DIR either from the working directory, or from the environment
961 # variable.
962 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
963 if [ -z "$GIT_DIR" ]; then
964 echol >&2 "fatal: mail.sh: GIT_DIR not set"
965 exit 1
968 projectdesc=
969 ! [ -s "$GIT_DIR/description" ] || projectdesc=$(LC_ALL=C sed -ne '1p' "$GIT_DIR/description")
970 # Check if the description is unchanged from it's default, and shorten it to
971 # a more manageable length if it is
972 if [ -z "$projectdesc" ] || LC_ALL=C expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
973 then
974 projectdesc="UNNAMED PROJECT"
977 # If --stdout is first argument send all output there instead of mailing
978 if [ "$1" = "--stdout" ]; then
979 shift
980 cfg_sendmail_bin="sendmail_to_stdout"
983 projectname="${4%.git}"
984 if [ -n "$projectname" ]; then
985 projectname="$projectname.git"
986 projectowner="$(config_get owner)"
987 else
988 projectname="$(basename "$PWD")"
990 projectboth="$projectname (\"$projectdesc\")"
991 projurl="$cfg_gitweburl/$projectname"
992 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
994 emailsender="$5"
995 emailextraheader="$6"
997 recipients=$(git config hooks.mailinglist)
998 summaryonly=$(git config --bool hooks.summaryonly 2>/dev/null || :)
999 reverseopt=
1000 [ "$(git config --bool hooks.reverseorder 2>/dev/null || :)" != "true" ] || reverseopt=--reverse
1001 announcerecipients=$(git config hooks.announcelist)
1002 envelopesender=$(git config hooks.envelopesender)
1003 emailprefix=$(git config hooks.emailprefix || echol "[$cfg_name] ")
1004 custom_showrev=$(git config hooks.showrev)
1006 # --- Main loop
1007 # Allow dual mode: run from the command line just like the update hook, or
1008 # if no arguments are given then run as a hook script
1009 # If --stdout is first argument send all output there instead (handled above)
1010 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
1011 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
1012 # Handle a single update rather than a batch on stdin
1013 # Output will still be sent to sendmail unless --stdout is used
1014 # Same 3 args as update hook (<refname> <old> <new>)
1015 if prep_for_email $2 $3 $1; then
1016 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1018 else
1019 # MAIL_SH_OTHER_BRANCHES cannot possibly be valid for multiple updates
1020 unset MAIL_SH_OTHER_BRANCHES
1021 # Same input as pre-receive hook (each line is <old> <new> <refname>)
1022 while read oldrev newrev refname
1024 prep_for_email $oldrev $newrev $refname || continue
1025 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1026 done
1029 exit 0