mail.sh: show a more sane summary of changes for new branches
[girocco.git] / taskd / mail.sh
blob94fd2738a28ff403681263b756e07505909c7bda
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 from the git hooks/post-receive script. It was
347 generated because a ref change was pushed to the repository containing
348 the project $projectname.
350 The $refname_type, $short_refname has been ${change_type}d
354 generate_email_footer()
356 SPACE=" "
357 cat <<-EOF
360 $cfg_name automatic notification. Contact project admin $projectowner
361 if you want to unsubscribe, or site admin $cfg_admin if you receive
362 no reply.
363 --${SPACE}
364 $projectboth
368 # --------------- Branches
371 # Called for the creation of a branch
373 generate_create_branch_email()
375 # This is a new branch and so oldrev is not valid
376 echol " at $newrev ($newrev_type)"
377 echol ""
379 echol "Those revisions in this branch that are new to this repository"
380 echol "have not appeared on any other notification email; so we list"
381 echol "those revisions in full, below."
383 echol ""
384 echol $LOGBEGIN
385 show_new_revisions
386 echol $LOGEND
388 # If any revisions were shown by show_new_revisions then we show
389 # a diffstat from the last shown revisions's parent (or the empty tree
390 # if the last revision shown is a root revision) to the new revision
391 # provided the last shown revision is not a merge commit.
392 # This is to show the truth of what happened in this change.
394 if [ -n "$LAST_SHOWN_REVISION" ] && [ -n "$LAST_SHOWN_NPARENTS" ] && [ "$LAST_SHOWN_NPARENTS" -le 1 ]; then
395 if [ "$LAST_SHOWN_NPARENTS" -eq 0 ]; then
396 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
397 # whether or not it's actually present in the repository.
399 # Set oldrev to the result of $(git hash-object -t tree --stdin < /dev/null)
400 oldrev="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
401 else
402 # Set oldrev to the parent of the last shown revision
403 oldrev="$LAST_SHOWN_REVISION^"
405 echol ""
406 echol "Summary of changes:"
407 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev $newrev
412 # Called for the change of a pre-existing branch
414 generate_update_branch_email()
416 # Consider this:
417 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
419 # O is $oldrev for $refname
420 # N is $newrev for $refname
421 # X is a revision pointed to by some other ref, for which we may
422 # assume that an email has already been generated.
423 # In this case we want to issue an email containing only revisions
424 # 3, 4, and N. Given (almost) by
426 # git rev-list N ^O --not --all
428 # The reason for the "almost", is that the "--not --all" will take
429 # precedence over the "N", and effectively will translate to
431 # git rev-list N ^O ^X ^N
433 # So, we need to build up the list more carefully. git rev-parse
434 # will generate a list of revs that may be fed into git rev-list.
435 # We can get it to make the "--not --all" part and then filter out
436 # the "^N" with:
438 # git rev-parse --not --all | grep -v N
440 # Then, using the --stdin switch to git rev-list we have effectively
441 # manufactured
443 # git rev-list N ^O ^X
445 # This leaves a problem when someone else updates the repository
446 # while this script is running. Their new value of the ref we're
447 # working on would be included in the "--not --all" output; and as
448 # our $newrev would be an ancestor of that commit, it would exclude
449 # all of our commits. What we really want is to exclude the current
450 # value of $refname from the --not list, rather than N itself. So:
452 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
454 # Get's us to something pretty safe (apart from the small time
455 # between refname being read, and git rev-parse running - for that,
456 # I give up)
459 # Next problem, consider this:
460 # * --- B --- * --- O ($oldrev)
462 # * --- X --- * --- N ($newrev)
464 # That is to say, there is no guarantee that oldrev is a strict
465 # subset of newrev (it would have required a --force, but that's
466 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
467 # Instead we find the common base of the two revs and list from
468 # there.
470 # As above, we need to take into account the presence of X; if
471 # another branch is already in the repository and points at some of
472 # the revisions that we are about to output - we don't want them.
473 # The solution is as before: git rev-parse output filtered.
475 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
477 # Tags pushed into the repository generate nice shortlog emails that
478 # summarise the commits between them and the previous tag. However,
479 # those emails don't include the full commit messages that we output
480 # for a branch update. Therefore we still want to output revisions
481 # that have been output on a tag email.
483 # Luckily, git rev-parse includes just the tool. Instead of using
484 # "--all" we use "--branches"; this has the added benefit that
485 # "remotes/" will be ignored as well.
487 # List all of the revisions that were removed by this update, in a
488 # fast-forward update, this list will be empty, because rev-list O
489 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
490 # revisions
491 fast_forward=""
492 rev=""
493 for rev in $(git rev-list $newrev..$oldrev)
495 revtype=$(git cat-file -t "$rev")
496 echol " discards $rev ($revtype)"
497 done
498 if [ -z "$rev" ]; then
499 fast_forward=1
502 # List all the revisions from baserev to newrev in a kind of
503 # "table-of-contents"; note this list can include revisions that
504 # have already had notification emails and is present to show the
505 # full detail of the change from rolling back the old revision to
506 # the base revision and then forward to the new revision
507 for rev in $(git rev-list $oldrev..$newrev)
509 revtype=$(git cat-file -t "$rev")
510 echol " via $rev ($revtype)"
511 done
513 if [ "$fast_forward" ]; then
514 echol " from $oldrev ($oldrev_type)"
515 else
516 # 1. Existing revisions were removed. In this case newrev
517 # is a subset of oldrev - this is the reverse of a
518 # fast-forward, a rewind
519 # 2. New revisions were added on top of an old revision,
520 # this is a rewind and addition.
522 # (1) certainly happened, (2) possibly. When (2) hasn't
523 # happened, we set a flag to indicate that no log printout
524 # is required.
526 echol ""
528 # Find the common ancestor of the old and new revisions and
529 # compare it with newrev
530 baserev=$(git merge-base $oldrev $newrev)
531 rewind_only=""
532 if [ "$baserev" = "$newrev" ]; then
533 echol "This update discarded existing revisions and left the branch pointing at"
534 echol "a previous point in the repository history."
535 echol ""
536 echol " * -- * -- N ($newrev)"
537 echol " \\"
538 echol " O -- O -- O ($oldrev)"
539 echol ""
540 echol "The removed revisions are not necessarily gone - if another reference"
541 echol "still refers to them they will stay in the repository."
542 rewind_only=1
543 else
544 echol "This update added new revisions after undoing existing revisions. That is"
545 echol "to say, the old revision is not a strict subset of the new revision. This"
546 echol "situation occurs when you --force push a change and generate a repository"
547 echol "containing something like this:"
548 echol ""
549 echol " * -- * -- B -- O -- O -- O ($oldrev)"
550 echol " \\"
551 echol " N -- N -- N ($newrev)"
552 echol ""
553 echol "When this happens we assume that you've already had alert emails for all"
554 echol "of the O revisions, and so we here report only the revisions in the N"
555 echol "branch from the common base, B."
559 echol ""
560 if [ -z "$rewind_only" ]; then
561 echol "Those revisions listed above that are new to this repository have"
562 echol "not appeared on any other notification email; so we list those"
563 echol "revisions in full, below."
565 echol ""
566 echol $LOGBEGIN
567 show_new_revisions
569 # XXX: Need a way of detecting whether git rev-list actually
570 # outputted anything, so that we can issue a "no new
571 # revisions added by this update" message
573 echol $LOGEND
574 else
575 echol "No new revisions were added by this update."
578 # The diffstat is shown from the old revision to the new revision.
579 # This is to show the truth of what happened in this change.
580 # There's no point showing the stat from the base to the new
581 # revision because the base is effectively a random revision at this
582 # point - the user will be interested in what this revision changed
583 # - including the undoing of previous revisions in the case of
584 # non-fast-forward updates.
585 echol ""
586 echol "Summary of changes:"
587 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev..$newrev
591 # Called for the deletion of a branch
593 generate_delete_branch_email()
595 echol " was $oldrev"
596 echol ""
597 echol $LOGBEGIN
598 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
599 echol $LOGEND
602 # --------------- Annotated tags
605 # Called for the creation of an annotated tag
607 generate_create_atag_email()
609 echol " at $newrev ($newrev_type)"
611 generate_atag_email
615 # Called for the update of an annotated tag (this is probably a rare event
616 # and may not even be allowed)
618 generate_update_atag_email()
620 echol " to $newrev ($newrev_type)"
621 echol " from $oldrev (which is now obsolete)"
623 generate_atag_email
627 # Called when an annotated tag is created or changed
629 generate_atag_email()
631 # Use read_tag_fields to pull out the individual fields from the
632 # tag and git cat-file --batch-check to fully peel the tag if needed
633 tagrev="$newrev"
634 tagrev16="$newrev16"
635 read_tag_fields "$tagrev"
636 if [ "$tagtype" = "tag" ]; then
637 # fully peel the tag
638 peeledobject=
639 peeledtype=
640 info="$(echo "$tagrev^{}" | git cat-file --batch-check='
641 peeledobject=%(objectname)
642 peeledtype=%(objecttype)'
644 case "$info" in *type=*) eval "$info";; *)
645 peeledtype=missing
646 esac
647 else
648 peeledobject="$tagobject"
649 peeledtype="$tagtype"
652 echol " tagging $peeledobject ($peeledtype)"
653 case "$peeledtype" in
654 commit)
656 # If the tagged object is a commit, then we assume this is a
657 # release, and so we calculate which tag this tag is
658 # replacing
659 prevtag=$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)
661 if [ -n "$prevtag" ]; then
662 echol " replaces $prevtag"
666 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
668 esac
669 echol " tagged by $taggername"
670 echol " on $taggerdate"
672 echol ""
673 echol $LOGBEGIN
675 while
676 echol "tag $tagrev"
677 echol "Tag: $tagtag"
678 echol "Object: $tagobject ($tagtype)"
679 [ -z "$taggername" ] || \
680 echol "Tagger: $taggername"
681 [ -z "$taggerdate" ] || \
682 echol "Date: $taggerdate"
683 echol "URL: <$projurl/$tagrev16>"
684 echol ""
686 # Show the content of the tag message; this might contain a change
687 # log or release notes so is worth displaying.
688 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
689 echol ""
690 [ "$tagtype" = "tag" ]
692 tagrev="$tagobject"
693 scratch="${tagrev#????????????????}"
694 tagrev16="${tagrev%$scratch}"
695 read_tag_fields "$tagrev" || break
696 done
698 case "$peeledtype" in
699 commit)
700 # Only commit tags make sense to have rev-list operations
701 # performed on them
702 if [ -n "$prevtag" ]; then
703 # Show changes since the previous release
704 git shortlog "$prevtag..$newrev"
705 else
706 # No previous tag, show all the changes since time
707 # began
708 git shortlog $newrev
712 # XXX: Is there anything useful we can do for non-commit
713 # objects?
715 esac
717 echol $LOGEND
721 # Called for the deletion of an annotated tag
723 generate_delete_atag_email()
725 echol " was $oldrev"
726 echol ""
727 echol $LOGBEGIN
728 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
729 echol $LOGEND
732 # --------------- General references
735 # Called when any other type of reference is created (most likely a
736 # non-annotated tag)
738 generate_create_general_email()
740 echol " at $newrev ($newrev_type)"
742 generate_general_email
746 # Called when any other type of reference is updated (most likely a
747 # non-annotated tag)
749 generate_update_general_email()
751 echol " to $newrev ($newrev_type)"
752 echol " from $oldrev"
754 generate_general_email
758 # Called for creation or update of any other type of reference
760 generate_general_email()
762 # Unannotated tags are more about marking a point than releasing a
763 # version; therefore we don't do the shortlog summary that we do for
764 # annotated tags above - we simply show that the point has been
765 # marked, and print the log message for the marked point for
766 # reference purposes
768 # Note this section also catches any other reference type (although
769 # there aren't any) and deals with them in the same way.
771 echol ""
772 if [ "$newrev_type" = "commit" ]; then
773 if [ -n "$(git rev-list --no-walk --merges $newrev)" ]; then
774 pfmt12="$pfmt1$pfmt1m$pfmt2"
775 else
776 pfmt12="$pfmt1$pfmt2"
778 echol $LOGBEGIN
779 git diff-tree --no-color --date=$datefmt --root -s --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$newrev16$pfmt3" --abbrev=$habbr $newrev
780 echol $LOGEND
781 else
782 # What can we do here? The tag marks an object that is not
783 # a commit, so there is no log for us to display. It's
784 # probably not wise to output git cat-file as it could be a
785 # binary blob. We'll just say how big it is
786 echol "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
791 # Called for the deletion of any other type of reference
793 generate_delete_general_email()
795 echol " was $oldrev"
796 echol ""
797 echol $LOGBEGIN
798 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
799 echol $LOGEND
803 # --------------- Miscellaneous utilities
806 # Show new revisions as the user would like to see them in the email.
808 # On return LAST_SHOWN_REVISION will be set to non-empty if any revisions shown
809 # and LAST_SHOWN_NPARENTS will be the number of parents it has (possibly 0)
811 show_new_revisions()
813 # This shows all log entries that are not already covered by
814 # another ref - i.e. commits that are now accessible from this
815 # ref that were previously not accessible
816 # (see generate_update_branch_email for the explanation of this
817 # command)
818 LAST_SHOWN_REVISION=
819 LAST_SHOWN_NPARENTS=
821 # Revision range passed to rev-list differs for new vs. updated
822 # branches.
823 if [ "$change_type" = create ]
824 then
825 # Show all revisions exclusive to this (new) branch.
826 revspec=$newrev
827 else
828 # Branch update; show revisions not part of $oldrev.
829 revspec=$oldrev..$newrev
832 if [ "${MAIL_SH_OTHER_BRANCHES+set}" = "set" ]; then
833 case "$MAIL_SH_OTHER_BRANCHES" in
835 othertips="git cat-file blob '${MAIL_SH_OTHER_BRANCHES#@}' 2>/dev/null"
838 othertips='printf "%s\n" $MAIL_SH_OTHER_BRANCHES'
840 esac
841 else
842 othertips='git for-each-ref --format="%(refname)" refs/heads |
843 LC_ALL=C awk -v "refname=$refname" "\$1 != refname"'
845 # if [ -z "$custom_showrev" ]
846 # then
847 # git rev-list --pretty --stdin $revspec
848 # else
849 while read onerev pcnt && [ -n "$onerev" ] && [ -n "$pcnt" ]
851 LAST_SHOWN_REVISION="$onerev"
852 LAST_SHOWN_NPARENTS="$pcnt"
853 if [ -n "$custom_showrev" ]; then
854 eval $(printf "$custom_showrev" $onerev)
855 else
856 if [ ${summaryonly:-false} = false ]; then
857 if [ ${pcnt:-1} -gt 1 ]; then
858 opts="-p --cc"
859 pfmt12="$pfmt1$pfmt1m$pfmt2"
860 else
861 opts="-p --stat=72 --summary"
862 pfmt12="$pfmt1$pfmt2"
864 else
865 if [ ${pcnt:-1} -gt 1 ]; then
866 opts="-s"
867 pfmt12="$pfmt1$pfmt1m$pfmt2"
868 else
869 opts="--stat=72 --summary"
870 pfmt12="$pfmt1$pfmt2"
873 scratch="${onerev#????????????????}"
874 onerev16="${onerev%$scratch}"
875 git diff-tree --no-color --date=$datefmt $opts --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$onerev16$pfmt3" --abbrev=$habbr -B -C --root $onerev
876 echo
878 done <<EOT
879 $(eval "$othertips" |
880 git cat-file --batch-check='^%(objectname)' |
881 LC_ALL=C sed -e '/ missing$/d' |
882 git --no-pager log --stdin --no-color --format=tformat:"%H %p" $revspec |
883 LC_ALL=C awk '{print $1 " " NF-1}')
885 # fi
889 size_limit()
891 size=0
892 while IFS= read -r line; do
893 # Include size of RFC 822 trailing CR+LF on each line
894 size=$(($size+${#line}+2))
895 if [ $size -gt $1 ]; then
896 echol "...e-mail trimmed, has been too large."
897 break
899 echol "$line"
900 done
904 sendmail_to_stdout()
910 send_mail()
912 if [ -n "$cfg_sender" ]; then
913 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
914 else
915 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
919 # ---------------------------- main()
921 # --- Constants
922 LOGBEGIN="- Log -----------------------------------------------------------------"
923 LOGEND="-----------------------------------------------------------------------"
925 # --- Config
926 . @basedir@/shlib.sh
928 # Git formatting to use
929 datefmt=rfc2822
930 habbr=12
932 # strftime formatting to use (empty is default rfc2822 format)
933 sdatefmt=
935 # This is --pretty=medium in four parts with a URL: line added
936 # The pfmt1m value must be inserted after the pfmt1 value but only for merges
937 # The URL must be inserted between pfmt2 and pfmt3
938 pfmt1='format:commit %H%n'
939 pfmt1m='Merge: %p%n'
940 pfmt2='Author: %an <%ae>%nDate: %ad%nURL: <'
941 pfmt3='>%n%n%w(0,4,4)%s%n%n%b'
943 git_add_config core.abbrev=$habbr
945 # Set GIT_DIR either from the working directory, or from the environment
946 # variable.
947 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
948 if [ -z "$GIT_DIR" ]; then
949 echol >&2 "fatal: post-receive: GIT_DIR not set"
950 exit 1
953 projectdesc=
954 ! [ -s "$GIT_DIR/description" ] || projectdesc=$(LC_ALL=C sed -ne '1p' "$GIT_DIR/description")
955 # Check if the description is unchanged from it's default, and shorten it to
956 # a more manageable length if it is
957 if [ -z "$projectdesc" ] || LC_ALL=C expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
958 then
959 projectdesc="UNNAMED PROJECT"
962 # If --stdout is first argument send all output there instead of mailing
963 if [ "$1" = "--stdout" ]; then
964 shift
965 cfg_sendmail_bin="sendmail_to_stdout"
968 projectname="${4%.git}"
969 if [ -n "$projectname" ]; then
970 projectname="$projectname.git"
971 projectowner="$(config_get owner)"
972 else
973 projectname="$(basename "$PWD")"
975 projectboth="$projectname (\"$projectdesc\")"
976 projurl="$cfg_gitweburl/$projectname"
977 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
979 emailsender="$5"
980 emailextraheader="$6"
982 recipients=$(git config hooks.mailinglist)
983 summaryonly=$(git config --bool hooks.summaryonly 2>/dev/null || :)
984 announcerecipients=$(git config hooks.announcelist)
985 envelopesender=$(git config hooks.envelopesender)
986 emailprefix=$(git config hooks.emailprefix || echol "[$cfg_name] ")
987 custom_showrev=$(git config hooks.showrev)
989 # --- Main loop
990 # Allow dual mode: run from the command line just like the update hook, or
991 # if no arguments are given then run as a hook script
992 # If --stdout is first argument send all output there instead (handled above)
993 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
994 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
995 # Handle a single update rather than a batch on stdin
996 # Output will still be sent to sendmail unless --stdout is used
997 # Same 3 args as update hook (<refname> <old> <new>)
998 if prep_for_email $2 $3 $1; then
999 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1001 else
1002 # MAIL_SH_OTHER_BRANCHES cannot possibly be valid for multiple updates
1003 unset MAIL_SH_OTHER_BRANCHES
1004 # Same input as pre-receive hook (each line is <old> <new> <refname>)
1005 while read oldrev newrev refname
1007 prep_for_email $oldrev $newrev $refname || continue
1008 PAGER= generate_email | size_limit $((256*1024)) | send_mail
1009 done
1012 exit 0