mail.sh: restore "Merge: ..." line for merge commits
[girocco.git] / taskd / mail.sh
blob1063b6b7a9cd0c7b41b9f1e9251d5b3a287180b3
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 # guess at what those are -- it's ignored if updates are piped in via stdin.
23 # * Load shlib.
24 # * Default subject prefix is site name.
25 # * Unsubscribe instructions in email footer.
26 # * Default showrev includes gitweb link and show -C.
27 # * Nicer subject line.
28 # * Limit mail size to 256kb.
29 # =================
31 # This hook is stored in the contrib/hooks directory. Your distribution
32 # will have put this somewhere standard. You should make this script
33 # executable then link to it in the repository you would like to use it in.
34 # For example, on debian the hook is stored in
35 # /usr/share/git-core/contrib/hooks/post-receive-email:
37 # chmod a+x post-receive-email
38 # cd /path/to/your/repository.git
39 # ln -sf /usr/share/git-core/contrib/hooks/post-receive-email hooks/post-receive
41 # This hook script assumes it is enabled on the central repository of a
42 # project, with all users pushing only to it and not between each other. It
43 # will still work if you don't operate in that style, but it would become
44 # possible for the email to be from someone other than the person doing the
45 # push.
47 # Config
48 # ------
49 # hooks.mailinglist
50 # This is the list that all pushes will go to; leave it blank to not send
51 # emails for every ref update.
52 # hooks.summaryonly
53 # If true do not include the actual diff when showing new commits (the
54 # summary information will still be shown). Default is false.
55 # hooks.announcelist
56 # This is the list that all pushes of annotated tags will go to. Leave it
57 # blank to default to the mailinglist field. The announce emails lists
58 # the short log summary of the changes since the last annotated tag.
59 # hooks.envelopesender
60 # If set then the -f option is passed to sendmail to allow the envelope
61 # sender address to be set
62 # hooks.emailprefix
63 # All emails have their subjects prefixed with this prefix, or "[SCM]"
64 # if emailprefix is unset, to aid filtering
65 # hooks.showrev
66 # The shell command used to format each revision in the email, with
67 # "%s" replaced with the commit id. Defaults to "git rev-list -1
68 # --pretty %s", displaying the commit id, author, date and log
69 # message. To list full patches separated by a blank line, you
70 # could set this to "git show -C %s; echo".
71 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
72 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
73 # Be careful if "..." contains things that will be expanded by shell "eval"
74 # or printf.
76 # Notes
77 # -----
78 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
79 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
80 # give information for debugging.
83 # ---------------------------- Functions
86 # Function to output arguments without interpretation followed by \n
88 echol()
90 printf '%s\n' "$*"
94 # Function to read a tag's (the single argument) header fields into
95 # tagobject the "object" value
96 # tagtype the "type" value
97 # tagtag the "tag" value
98 # taggername from "tagger" value before first '<'
99 # taggerdate from "tagger" value after '>' but formatted into a date string
100 # taggeremail from "tagger" value between '<' and '>'
101 # On return all fields will be set (to empty if failure) and the three tagger
102 # fields will only be set to non-empty if a "tagger" header field is present.
103 # If the object does not exist or is not a tag the function returns failure.
104 read_tag_fields()
106 tagobject=
107 tagtype=
108 tagtag=
109 taggername=
110 taggerdate=
111 taggeremail=
112 if _tagfields="$(git cat-file tag "$1" | LC_ALL=C awk -F '[ ]' '
113 function quotesh(sv) {
114 gsub(/'\''/, "'\'\\\\\'\''", sv)
115 return "'\''" sv "'\''"
117 function trim(sv) {
118 sub(/^[ \t]+/, "", sv)
119 sub(/[ \t]+$/, "", sv)
120 return sv
122 function hval(sv) {
123 sub(/^[^ ]* /, "", sv)
124 return sv
126 NR==1,/^$/ {
127 if ($1 == "object") to=hval($0)
128 if ($1 == "type") ty=hval($0)
129 if ($1 == "tag") tt=hval($0)
130 if ($1 == "tagger") tr=hval($0)
132 END {
133 if (tt == "" || to == "" || ty == "") exit 1
134 print "tagobject=" quotesh(to)
135 print "tagtype=" quotesh(ty)
136 print "tagtag=" quotesh(tt)
137 if (match(tr, /^[ \t]*[^ \t<][^<]*/)) {
138 tn=substr(tr, RSTART, RLENGTH)
139 tr=substr(tr, RSTART + RLENGTH)
140 tn=trim(tn)
141 if (tn != "")
142 print "taggername=" quotesh(tn)
143 if (match(tr, /^<.*>/)) {
144 if (RLENGTH > 2)
145 te=substr(tr, RSTART+1, RLENGTH-2)
146 tr=substr(tr, RSTART + RLENGTH)
147 te=trim(te)
148 if (te != "")
149 print "taggeremail=" quotesh(te)
150 if (match(tr, /^[ \t]*[0-9]+([ \t]+[-+]?[0-9][0-9]([0-9][0-9]([0-9][0-9])?)?)[ \t]*$/))
151 td=trim(tr)
152 if (td != "")
153 print "taggerdate=" quotesh(td)
157 ')"; then
158 eval "$_tagfields"
159 [ -z "$taggerdate" ] || taggerdate="$(strftime "$sdatefmt" $taggerdate)"
160 return 0
162 return 1
166 # Function to prepare for email generation. This decides what type
167 # of update this is and whether an email should even be generated.
169 prep_for_email()
171 # --- Arguments
172 oldrev=$(git rev-parse --revs-only "$1" --)
173 newrev=$(git rev-parse --revs-only "$2" --)
174 [ "$oldrev" != "$newrev" ] || return 1
175 scratch="${newrev#????????????????}"
176 newrev16="${newrev%$scratch}"
177 refname="$3"
179 # --- Interpret
180 # 0000->1234 (create)
181 # 1234->2345 (update)
182 # 2345->0000 (delete)
183 if LC_ALL=C expr "$oldrev" : '0*$' >/dev/null
184 then
185 change_type="create"
186 else
187 if LC_ALL=C expr "$newrev" : '0*$' >/dev/null
188 then
189 change_type="delete"
190 else
191 change_type="update"
195 # --- Get the revision types
196 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
197 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
198 case "$change_type" in
199 create|update)
200 rev="$newrev"
201 rev_type="$newrev_type"
203 delete)
204 rev="$oldrev"
205 rev_type="$oldrev_type"
207 esac
209 # The revision type tells us what type the commit is, combined with
210 # the location of the ref we can decide between
211 # - working branch
212 # - tracking branch
213 # - unannoted tag
214 # - annotated tag
215 case "$refname","$rev_type" in
216 refs/tags/*,commit)
217 # un-annotated tag
218 refname_type="tag"
219 short_refname=${refname##refs/tags/}
221 refs/tags/*,tag)
222 # annotated tag
223 refname_type="annotated tag"
224 short_refname=${refname##refs/tags/}
225 # change recipients
226 if [ -n "$announcerecipients" ]; then
227 recipients="$announcerecipients"
230 refs/heads/*,commit|refs/heads/*,tag)
231 # branch
232 refname_type="branch"
233 short_refname=${refname##refs/heads/}
235 refs/remotes/*,commit)
236 # tracking branch
237 refname_type="tracking branch"
238 short_refname=${refname##refs/remotes/}
239 echol >&2 "*** Push-update of tracking branch, $refname"
240 echol >&2 "*** - no email generated."
241 return 1
243 refs/mob/*,commit|refs/mob/*,tag)
244 # personal mob ref
245 refname_type="personal mob ref"
246 short_refname=${refname##refs/mob/}
247 echol >&2 "*** Push-update of personal mob ref, $refname"
248 echol >&2 "*** - no email generated."
249 return 1
252 # Anything else (is there anything else?)
253 echol >&2 "*** Unknown type of update to $refname ($rev_type)"
254 echol >&2 "*** - no email generated"
255 return 1
257 esac
259 # Check if we've got anyone to send to
260 if [ -z "$recipients" ]; then
261 case "$refname_type" in
262 "annotated tag")
263 config_name="hooks.announcelist"
266 config_name="hooks.mailinglist"
268 esac
269 echol >&2 "*** $config_name is not set so no email will be sent"
270 echol >&2 "*** for $refname update $oldrev->$newrev"
271 return 1
274 return 0
278 # Top level email generation function. This calls the appropriate
279 # body-generation routine after outputting the common header.
281 # Note this function doesn't actually generate any email output, that is
282 # taken care of by the functions it calls:
283 # - generate_email_header
284 # - generate_create_XXXX_email
285 # - generate_update_XXXX_email
286 # - generate_delete_XXXX_email
287 # - generate_email_footer
289 # Note also that this function cannot 'exit' from the script; when this
290 # function is running (in hook script mode), the send_mail() function
291 # is already executing in another process, connected via a pipe, and
292 # if this function exits without, whatever has been generated to that
293 # point will be sent as an email... even if nothing has been generated.
295 generate_email()
297 # Email parameters
298 # The email subject will contain the best description of the ref
299 # that we can build from the parameters
300 describe=$(git describe $rev 2>/dev/null)
301 if [ -z "$describe" ]; then
302 describe=$rev
305 generate_email_header
307 # Call the correct body generation function
308 fn_name=general
309 case "$refname_type" in
310 "tracking branch"|branch)
311 fn_name=branch
313 "annotated tag")
314 fn_name=atag
316 esac
317 generate_${change_type}_${fn_name}_email
319 generate_email_footer
322 generate_email_header()
324 # --- Email (all stdout will be the email)
325 # Generate header
326 if [ -n "$emailsender" ]; then
327 echol "From: $emailsender"
329 cat <<-EOF
330 To: $recipients
331 Subject: ${emailprefix}$projectname $refname_type $short_refname ${change_type}d: $describe
332 MIME-Version: 1.0
333 Content-Type: text/plain; charset=utf-8
334 Content-Transfer-Encoding: 8bit
336 [ -n "$cfg_suppress_x_girocco" ] || echol "X-Girocco: $cfg_gitweburl"
337 [ -z "$emailextraheader" ] || echol "$emailextraheader"
338 cat <<-EOF
339 X-Git-Refname: $refname
340 X-Git-Reftype: $refname_type
341 X-Git-Oldrev: $oldrev
342 X-Git-Newrev: $newrev
343 Auto-Submitted: auto-generated
345 This is an automated email from the git hooks/post-receive script. It was
346 generated because a ref change was pushed to the repository containing
347 the 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 # The diffstat is shown from an empty tree to the new revision.
388 # This is to show the truth of what happened in this change.
389 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
390 # whether or not it's actually present in the repository.
391 echol ""
392 echol "Summary of changes:"
393 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder 4b825dc642cb6eb9a060e54bf8d69288fbee4904 $newrev
397 # Called for the change of a pre-existing branch
399 generate_update_branch_email()
401 # Consider this:
402 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
404 # O is $oldrev for $refname
405 # N is $newrev for $refname
406 # X is a revision pointed to by some other ref, for which we may
407 # assume that an email has already been generated.
408 # In this case we want to issue an email containing only revisions
409 # 3, 4, and N. Given (almost) by
411 # git rev-list N ^O --not --all
413 # The reason for the "almost", is that the "--not --all" will take
414 # precedence over the "N", and effectively will translate to
416 # git rev-list N ^O ^X ^N
418 # So, we need to build up the list more carefully. git rev-parse
419 # will generate a list of revs that may be fed into git rev-list.
420 # We can get it to make the "--not --all" part and then filter out
421 # the "^N" with:
423 # git rev-parse --not --all | grep -v N
425 # Then, using the --stdin switch to git rev-list we have effectively
426 # manufactured
428 # git rev-list N ^O ^X
430 # This leaves a problem when someone else updates the repository
431 # while this script is running. Their new value of the ref we're
432 # working on would be included in the "--not --all" output; and as
433 # our $newrev would be an ancestor of that commit, it would exclude
434 # all of our commits. What we really want is to exclude the current
435 # value of $refname from the --not list, rather than N itself. So:
437 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
439 # Get's us to something pretty safe (apart from the small time
440 # between refname being read, and git rev-parse running - for that,
441 # I give up)
444 # Next problem, consider this:
445 # * --- B --- * --- O ($oldrev)
447 # * --- X --- * --- N ($newrev)
449 # That is to say, there is no guarantee that oldrev is a strict
450 # subset of newrev (it would have required a --force, but that's
451 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
452 # Instead we find the common base of the two revs and list from
453 # there.
455 # As above, we need to take into account the presence of X; if
456 # another branch is already in the repository and points at some of
457 # the revisions that we are about to output - we don't want them.
458 # The solution is as before: git rev-parse output filtered.
460 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
462 # Tags pushed into the repository generate nice shortlog emails that
463 # summarise the commits between them and the previous tag. However,
464 # those emails don't include the full commit messages that we output
465 # for a branch update. Therefore we still want to output revisions
466 # that have been output on a tag email.
468 # Luckily, git rev-parse includes just the tool. Instead of using
469 # "--all" we use "--branches"; this has the added benefit that
470 # "remotes/" will be ignored as well.
472 # List all of the revisions that were removed by this update, in a
473 # fast-forward update, this list will be empty, because rev-list O
474 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
475 # revisions
476 fast_forward=""
477 rev=""
478 for rev in $(git rev-list $newrev..$oldrev)
480 revtype=$(git cat-file -t "$rev")
481 echol " discards $rev ($revtype)"
482 done
483 if [ -z "$rev" ]; then
484 fast_forward=1
487 # List all the revisions from baserev to newrev in a kind of
488 # "table-of-contents"; note this list can include revisions that
489 # have already had notification emails and is present to show the
490 # full detail of the change from rolling back the old revision to
491 # the base revision and then forward to the new revision
492 for rev in $(git rev-list $oldrev..$newrev)
494 revtype=$(git cat-file -t "$rev")
495 echol " via $rev ($revtype)"
496 done
498 if [ "$fast_forward" ]; then
499 echol " from $oldrev ($oldrev_type)"
500 else
501 # 1. Existing revisions were removed. In this case newrev
502 # is a subset of oldrev - this is the reverse of a
503 # fast-forward, a rewind
504 # 2. New revisions were added on top of an old revision,
505 # this is a rewind and addition.
507 # (1) certainly happened, (2) possibly. When (2) hasn't
508 # happened, we set a flag to indicate that no log printout
509 # is required.
511 echol ""
513 # Find the common ancestor of the old and new revisions and
514 # compare it with newrev
515 baserev=$(git merge-base $oldrev $newrev)
516 rewind_only=""
517 if [ "$baserev" = "$newrev" ]; then
518 echol "This update discarded existing revisions and left the branch pointing at"
519 echol "a previous point in the repository history."
520 echol ""
521 echol " * -- * -- N ($newrev)"
522 echol " \\"
523 echol " O -- O -- O ($oldrev)"
524 echol ""
525 echol "The removed revisions are not necessarily gone - if another reference"
526 echol "still refers to them they will stay in the repository."
527 rewind_only=1
528 else
529 echol "This update added new revisions after undoing existing revisions. That is"
530 echol "to say, the old revision is not a strict subset of the new revision. This"
531 echol "situation occurs when you --force push a change and generate a repository"
532 echol "containing something like this:"
533 echol ""
534 echol " * -- * -- B -- O -- O -- O ($oldrev)"
535 echol " \\"
536 echol " N -- N -- N ($newrev)"
537 echol ""
538 echol "When this happens we assume that you've already had alert emails for all"
539 echol "of the O revisions, and so we here report only the revisions in the N"
540 echol "branch from the common base, B."
544 echol ""
545 if [ -z "$rewind_only" ]; then
546 echol "Those revisions listed above that are new to this repository have"
547 echol "not appeared on any other notification email; so we list those"
548 echol "revisions in full, below."
550 echol ""
551 echol $LOGBEGIN
552 show_new_revisions
554 # XXX: Need a way of detecting whether git rev-list actually
555 # outputted anything, so that we can issue a "no new
556 # revisions added by this update" message
558 echol $LOGEND
559 else
560 echol "No new revisions were added by this update."
563 # The diffstat is shown from the old revision to the new revision.
564 # This is to show the truth of what happened in this change.
565 # There's no point showing the stat from the base to the new
566 # revision because the base is effectively a random revision at this
567 # point - the user will be interested in what this revision changed
568 # - including the undoing of previous revisions in the case of
569 # non-fast-forward updates.
570 echol ""
571 echol "Summary of changes:"
572 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev..$newrev
576 # Called for the deletion of a branch
578 generate_delete_branch_email()
580 echol " was $oldrev"
581 echol ""
582 echol $LOGBEGIN
583 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
584 echol $LOGEND
587 # --------------- Annotated tags
590 # Called for the creation of an annotated tag
592 generate_create_atag_email()
594 echol " at $newrev ($newrev_type)"
596 generate_atag_email
600 # Called for the update of an annotated tag (this is probably a rare event
601 # and may not even be allowed)
603 generate_update_atag_email()
605 echol " to $newrev ($newrev_type)"
606 echol " from $oldrev (which is now obsolete)"
608 generate_atag_email
612 # Called when an annotated tag is created or changed
614 generate_atag_email()
616 # Use read_tag_fields to pull out the individual fields from the
617 # tag and git cat-file --batch-check to fully peel the tag if needed
618 tagrev="$newrev"
619 tagrev16="$newrev16"
620 read_tag_fields "$tagrev"
621 if [ "$tagtype" = "tag" ]; then
622 # fully peel the tag
623 peeledobject=
624 peeledtype=
625 info="$(echo "$tagrev^{}" | git cat-file --batch-check='
626 peeledobject=%(objectname)
627 peeledtype=%(objecttype)'
629 case "$info" in *type=*) eval "$info";; *)
630 peeledtype=missing
631 esac
632 else
633 peeledobject="$tagobject"
634 peeledtype="$tagtype"
637 echol " tagging $peeledobject ($peeledtype)"
638 case "$peeledtype" in
639 commit)
641 # If the tagged object is a commit, then we assume this is a
642 # release, and so we calculate which tag this tag is
643 # replacing
644 prevtag=$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)
646 if [ -n "$prevtag" ]; then
647 echol " replaces $prevtag"
651 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
653 esac
654 echol " tagged by $taggername"
655 echol " on $taggerdate"
657 echol ""
658 echol $LOGBEGIN
660 while
661 echol "tag $tagrev"
662 echol "Tag: $tagtag"
663 echol "Object: $tagobject ($tagtype)"
664 [ -z "$taggername" ] || \
665 echol "Tagger: $taggername"
666 [ -z "$taggerdate" ] || \
667 echol "Date: $taggerdate"
668 echol "URL: <$projurl/$tagrev16>"
669 echol ""
671 # Show the content of the tag message; this might contain a change
672 # log or release notes so is worth displaying.
673 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
674 echol ""
675 [ "$tagtype" = "tag" ]
677 tagrev="$tagobject"
678 scratch="${tagrev#????????????????}"
679 tagrev16="${tagrev%$scratch}"
680 read_tag_fields "$tagrev" || break
681 done
683 case "$peeledtype" in
684 commit)
685 # Only commit tags make sense to have rev-list operations
686 # performed on them
687 if [ -n "$prevtag" ]; then
688 # Show changes since the previous release
689 git shortlog "$prevtag..$newrev"
690 else
691 # No previous tag, show all the changes since time
692 # began
693 git shortlog $newrev
697 # XXX: Is there anything useful we can do for non-commit
698 # objects?
700 esac
702 echol $LOGEND
706 # Called for the deletion of an annotated tag
708 generate_delete_atag_email()
710 echol " was $oldrev"
711 echol ""
712 echol $LOGBEGIN
713 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
714 echol $LOGEND
717 # --------------- General references
720 # Called when any other type of reference is created (most likely a
721 # non-annotated tag)
723 generate_create_general_email()
725 echol " at $newrev ($newrev_type)"
727 generate_general_email
731 # Called when any other type of reference is updated (most likely a
732 # non-annotated tag)
734 generate_update_general_email()
736 echol " to $newrev ($newrev_type)"
737 echol " from $oldrev"
739 generate_general_email
743 # Called for creation or update of any other type of reference
745 generate_general_email()
747 # Unannotated tags are more about marking a point than releasing a
748 # version; therefore we don't do the shortlog summary that we do for
749 # annotated tags above - we simply show that the point has been
750 # marked, and print the log message for the marked point for
751 # reference purposes
753 # Note this section also catches any other reference type (although
754 # there aren't any) and deals with them in the same way.
756 echol ""
757 if [ "$newrev_type" = "commit" ]; then
758 if [ -n "$(git rev-list --no-walk --merges $newrev)" ]; then
759 pfmt12="$pfmt1$pfmt1m$pfmt2"
760 else
761 pfmt12="$pfmt1$pfmt2"
763 echol $LOGBEGIN
764 git diff-tree --no-color --date=$datefmt --root -s --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$newrev16$pfmt3" $newrev
765 echol $LOGEND
766 else
767 # What can we do here? The tag marks an object that is not
768 # a commit, so there is no log for us to display. It's
769 # probably not wise to output git cat-file as it could be a
770 # binary blob. We'll just say how big it is
771 echol "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
776 # Called for the deletion of any other type of reference
778 generate_delete_general_email()
780 echol " was $oldrev"
781 echol ""
782 echol $LOGBEGIN
783 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
784 echol $LOGEND
788 # --------------- Miscellaneous utilities
791 # Show new revisions as the user would like to see them in the email.
793 show_new_revisions()
795 # This shows all log entries that are not already covered by
796 # another ref - i.e. commits that are now accessible from this
797 # ref that were previously not accessible
798 # (see generate_update_branch_email for the explanation of this
799 # command)
801 # Revision range passed to rev-list differs for new vs. updated
802 # branches.
803 if [ "$change_type" = create ]
804 then
805 # Show all revisions exclusive to this (new) branch.
806 revspec=$newrev
807 else
808 # Branch update; show revisions not part of $oldrev.
809 revspec=$oldrev..$newrev
812 if [ "${MAIL_SH_OTHER_BRANCHES+set}" = "set" ]; then
813 case "$MAIL_SH_OTHER_BRANCHES" in
815 othertips="git cat-file blob '${MAIL_SH_OTHER_BRANCHES#@}' 2>/dev/null"
818 othertips='printf "%s\n" $MAIL_SH_OTHER_BRANCHES'
820 esac
821 else
822 othertips='git for-each-ref --format="%(refname)" refs/heads |
823 LC_ALL=C awk -v "refname=$refname" "\$1 != refname"'
825 eval "$othertips" |
826 git cat-file --batch-check='^%(objectname)' |
827 LC_ALL=C sed -e '/ missing$/d' |
828 # if [ -z "$custom_showrev" ]
829 # then
830 # git rev-list --pretty --stdin $revspec
831 # else
832 git --no-pager log --stdin --no-color --format=tformat:"%H %p" $revspec |
833 LC_ALL=C awk '{print $1 " " NF-1}' |
834 while read onerev pcnt
836 if [ -n "$custom_showrev" ]; then
837 eval $(printf "$custom_showrev" $onerev)
838 else
839 if [ ${summaryonly:-false} = false ]; then
840 if [ ${pcnt:-1} -gt 1 ]; then
841 opts="-p --cc"
842 pfmt12="$pfmt1$pfmt1m$pfmt2"
843 else
844 opts="-p --stat=72 --summary"
845 pfmt12="$pfmt1$pfmt2"
847 else
848 if [ ${pcnt:-1} -gt 1 ]; then
849 opts="-s"
850 pfmt12="$pfmt1$pfmt1m$pfmt2"
851 else
852 opts="--stat=72 --summary"
853 pfmt12="$pfmt1$pfmt2"
856 scratch="${onerev#????????????????}"
857 onerev16="${onerev%$scratch}"
858 git diff-tree --no-color --date=$datefmt $opts --always --encoding=UTF-8 --format="$pfmt12$projurlesc/$onerev16$pfmt3" --abbrev=$habbr -B -C --root $onerev
859 echo
861 done
862 # fi
866 size_limit()
868 size=0
869 while IFS= read -r line; do
870 # Include size of RFC 822 trailing CR+LF on each line
871 size=$(($size+${#line}+2))
872 if [ $size -gt $1 ]; then
873 echol "...e-mail trimmed, has been too large."
874 break
876 echol "$line"
877 done
881 sendmail_to_stdout()
887 send_mail()
889 if [ -n "$cfg_sender" ]; then
890 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
891 else
892 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
896 # ---------------------------- main()
898 # --- Constants
899 LOGBEGIN="- Log -----------------------------------------------------------------"
900 LOGEND="-----------------------------------------------------------------------"
902 # --- Config
903 . @basedir@/shlib.sh
905 # Git formatting to use
906 datefmt=rfc2822
907 habbr=12
909 # strftime formatting to use (empty is default rfc2822 format)
910 sdatefmt=
912 # This is --pretty=medium in four parts with a URL: line added
913 # The pfmt1m value must be inserted after the pfmt1 value but only for merges
914 # The URL must be inserted between pfmt2 and pfmt3
915 pfmt1='format:commit %H%n'
916 pfmt1m='Merge: %p%n'
917 pfmt2='Author: %an <%ae>%nDate: %ad%nURL: <'
918 pfmt3='>%n%n%w(0,4,4)%s%n%n%b'
920 git_add_config core.abbrev=$habbr
922 # Set GIT_DIR either from the working directory, or from the environment
923 # variable.
924 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
925 if [ -z "$GIT_DIR" ]; then
926 echol >&2 "fatal: post-receive: GIT_DIR not set"
927 exit 1
930 projectdesc=
931 ! [ -s "$GIT_DIR/description" ] || projectdesc=$(LC_ALL=C sed -ne '1p' "$GIT_DIR/description")
932 # Check if the description is unchanged from it's default, and shorten it to
933 # a more manageable length if it is
934 if [ -z "$projectdesc" ] || LC_ALL=C expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
935 then
936 projectdesc="UNNAMED PROJECT"
939 # If --stdout is first argument send all output there instead of mailing
940 if [ "$1" = "--stdout" ]; then
941 shift
942 cfg_sendmail_bin="sendmail_to_stdout"
945 projectname="${4%.git}"
946 if [ -n "$projectname" ]; then
947 projectname="$projectname.git"
948 projectowner="$(config_get owner)"
949 else
950 projectname="$(basename "$PWD")"
952 projectboth="$projectname (\"$projectdesc\")"
953 projurl="$cfg_gitweburl/$projectname"
954 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
956 emailsender="$5"
957 emailextraheader="$6"
959 recipients=$(git config hooks.mailinglist)
960 summaryonly=$(git config --bool hooks.summaryonly 2>/dev/null || :)
961 announcerecipients=$(git config hooks.announcelist)
962 envelopesender=$(git config hooks.envelopesender)
963 emailprefix=$(git config hooks.emailprefix || echol "[$cfg_name] ")
964 custom_showrev=$(git config hooks.showrev)
966 # --- Main loop
967 # Allow dual mode: run from the command line just like the update hook, or
968 # if no arguments are given then run as a hook script
969 # If --stdout is first argument send all output there instead (handled above)
970 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
971 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
972 # Handle a single update rather than a batch on stdin
973 # Output will still be sent to sendmail unless --stdout is used
974 # Same 3 args as update hook (<refname> <old> <new>)
975 if prep_for_email $2 $3 $1; then
976 PAGER= generate_email | size_limit $((256*1024)) | send_mail
978 else
979 # MAIL_SH_OTHER_BRANCHES cannot possibly be valid for multiple updates
980 unset MAIL_SH_OTHER_BRANCHES
981 # Same input as pre-receive hook (each line is <old> <new> <refname>)
982 while read oldrev newrev refname
984 prep_for_email $oldrev $newrev $refname || continue
985 PAGER= generate_email | size_limit $((256*1024)) | send_mail
986 done
989 exit 0