strftime.c: update to latest
[girocco.git] / taskd / mail.sh
blob80e27b28c425b5225a9291ba76691c8a9bba6e14
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 $LOGBEGIN
379 show_new_revisions
380 echol $LOGEND
382 # The diffstat is shown from an empty tree to the new revision.
383 # This is to show the truth of what happened in this change.
384 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
385 # whether or not it's actually present in the repository.
386 echol ""
387 echol "Summary of changes:"
388 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder 4b825dc642cb6eb9a060e54bf8d69288fbee4904 $newrev
392 # Called for the change of a pre-existing branch
394 generate_update_branch_email()
396 # Consider this:
397 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
399 # O is $oldrev for $refname
400 # N is $newrev for $refname
401 # X is a revision pointed to by some other ref, for which we may
402 # assume that an email has already been generated.
403 # In this case we want to issue an email containing only revisions
404 # 3, 4, and N. Given (almost) by
406 # git rev-list N ^O --not --all
408 # The reason for the "almost", is that the "--not --all" will take
409 # precedence over the "N", and effectively will translate to
411 # git rev-list N ^O ^X ^N
413 # So, we need to build up the list more carefully. git rev-parse
414 # will generate a list of revs that may be fed into git rev-list.
415 # We can get it to make the "--not --all" part and then filter out
416 # the "^N" with:
418 # git rev-parse --not --all | grep -v N
420 # Then, using the --stdin switch to git rev-list we have effectively
421 # manufactured
423 # git rev-list N ^O ^X
425 # This leaves a problem when someone else updates the repository
426 # while this script is running. Their new value of the ref we're
427 # working on would be included in the "--not --all" output; and as
428 # our $newrev would be an ancestor of that commit, it would exclude
429 # all of our commits. What we really want is to exclude the current
430 # value of $refname from the --not list, rather than N itself. So:
432 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
434 # Get's us to something pretty safe (apart from the small time
435 # between refname being read, and git rev-parse running - for that,
436 # I give up)
439 # Next problem, consider this:
440 # * --- B --- * --- O ($oldrev)
442 # * --- X --- * --- N ($newrev)
444 # That is to say, there is no guarantee that oldrev is a strict
445 # subset of newrev (it would have required a --force, but that's
446 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
447 # Instead we find the common base of the two revs and list from
448 # there.
450 # As above, we need to take into account the presence of X; if
451 # another branch is already in the repository and points at some of
452 # the revisions that we are about to output - we don't want them.
453 # The solution is as before: git rev-parse output filtered.
455 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
457 # Tags pushed into the repository generate nice shortlog emails that
458 # summarise the commits between them and the previous tag. However,
459 # those emails don't include the full commit messages that we output
460 # for a branch update. Therefore we still want to output revisions
461 # that have been output on a tag email.
463 # Luckily, git rev-parse includes just the tool. Instead of using
464 # "--all" we use "--branches"; this has the added benefit that
465 # "remotes/" will be ignored as well.
467 # List all of the revisions that were removed by this update, in a
468 # fast-forward update, this list will be empty, because rev-list O
469 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
470 # revisions
471 fast_forward=""
472 rev=""
473 for rev in $(git rev-list $newrev..$oldrev)
475 revtype=$(git cat-file -t "$rev")
476 echol " discards $rev ($revtype)"
477 done
478 if [ -z "$rev" ]; then
479 fast_forward=1
482 # List all the revisions from baserev to newrev in a kind of
483 # "table-of-contents"; note this list can include revisions that
484 # have already had notification emails and is present to show the
485 # full detail of the change from rolling back the old revision to
486 # the base revision and then forward to the new revision
487 for rev in $(git rev-list $oldrev..$newrev)
489 revtype=$(git cat-file -t "$rev")
490 echol " via $rev ($revtype)"
491 done
493 if [ "$fast_forward" ]; then
494 echol " from $oldrev ($oldrev_type)"
495 else
496 # 1. Existing revisions were removed. In this case newrev
497 # is a subset of oldrev - this is the reverse of a
498 # fast-forward, a rewind
499 # 2. New revisions were added on top of an old revision,
500 # this is a rewind and addition.
502 # (1) certainly happened, (2) possibly. When (2) hasn't
503 # happened, we set a flag to indicate that no log printout
504 # is required.
506 echol ""
508 # Find the common ancestor of the old and new revisions and
509 # compare it with newrev
510 baserev=$(git merge-base $oldrev $newrev)
511 rewind_only=""
512 if [ "$baserev" = "$newrev" ]; then
513 echol "This update discarded existing revisions and left the branch pointing at"
514 echol "a previous point in the repository history."
515 echol ""
516 echol " * -- * -- N ($newrev)"
517 echol " \\"
518 echol " O -- O -- O ($oldrev)"
519 echol ""
520 echol "The removed revisions are not necessarily gone - if another reference"
521 echol "still refers to them they will stay in the repository."
522 rewind_only=1
523 else
524 echol "This update added new revisions after undoing existing revisions. That is"
525 echol "to say, the old revision is not a strict subset of the new revision. This"
526 echol "situation occurs when you --force push a change and generate a repository"
527 echol "containing something like this:"
528 echol ""
529 echol " * -- * -- B -- O -- O -- O ($oldrev)"
530 echol " \\"
531 echol " N -- N -- N ($newrev)"
532 echol ""
533 echol "When this happens we assume that you've already had alert emails for all"
534 echol "of the O revisions, and so we here report only the revisions in the N"
535 echol "branch from the common base, B."
539 echol ""
540 if [ -z "$rewind_only" ]; then
541 echol "Those revisions listed above that are new to this repository have"
542 echol "not appeared on any other notification email; so we list those"
543 echol "revisions in full, below."
545 echol ""
546 echol $LOGBEGIN
547 show_new_revisions
549 # XXX: Need a way of detecting whether git rev-list actually
550 # outputted anything, so that we can issue a "no new
551 # revisions added by this update" message
553 echol $LOGEND
554 else
555 echol "No new revisions were added by this update."
558 # The diffstat is shown from the old revision to the new revision.
559 # This is to show the truth of what happened in this change.
560 # There's no point showing the stat from the base to the new
561 # revision because the base is effectively a random revision at this
562 # point - the user will be interested in what this revision changed
563 # - including the undoing of previous revisions in the case of
564 # non-fast-forward updates.
565 echol ""
566 echol "Summary of changes:"
567 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev..$newrev
571 # Called for the deletion of a branch
573 generate_delete_branch_email()
575 echol " was $oldrev"
576 echol ""
577 echol $LOGBEGIN
578 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
579 echol $LOGEND
582 # --------------- Annotated tags
585 # Called for the creation of an annotated tag
587 generate_create_atag_email()
589 echol " at $newrev ($newrev_type)"
591 generate_atag_email
595 # Called for the update of an annotated tag (this is probably a rare event
596 # and may not even be allowed)
598 generate_update_atag_email()
600 echol " to $newrev ($newrev_type)"
601 echol " from $oldrev (which is now obsolete)"
603 generate_atag_email
607 # Called when an annotated tag is created or changed
609 generate_atag_email()
611 # Use read_tag_fields to pull out the individual fields from the
612 # tag and git cat-file --batch-check to fully peel the tag if needed
613 tagrev="$newrev"
614 tagrev16="$newrev16"
615 read_tag_fields "$tagrev"
616 if [ "$tagtype" = "tag" ]; then
617 # fully peel the tag
618 peeledobject=
619 peeledtype=
620 info="$(echo "$tagrev^{}" | git cat-file --batch-check='
621 peeledobject=%(objectname)
622 peeledtype=%(objecttype)'
624 case "$info" in *type=*) eval "$info";; *)
625 peeledtype=missing
626 esac
627 else
628 peeledobject="$tagobject"
629 peeledtype="$tagtype"
632 echol " tagging $peeledobject ($peeledtype)"
633 case "$peeledtype" in
634 commit)
636 # If the tagged object is a commit, then we assume this is a
637 # release, and so we calculate which tag this tag is
638 # replacing
639 prevtag=$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)
641 if [ -n "$prevtag" ]; then
642 echol " replaces $prevtag"
646 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
648 esac
649 echol " tagged by $taggername"
650 echol " on $taggerdate"
652 echol ""
653 echol $LOGBEGIN
655 while
656 echol "tag $tagrev"
657 echol "Tag: $tagtag"
658 echol "Object: $tagobject ($tagtype)"
659 [ -z "$taggername" ] || \
660 echol "Tagger: $taggername"
661 [ -z "$taggerdate" ] || \
662 echol "Date: $taggerdate"
663 echol "URL: <$projurl/$tagrev16>"
664 echol ""
666 # Show the content of the tag message; this might contain a change
667 # log or release notes so is worth displaying.
668 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
669 echol ""
670 [ "$tagtype" = "tag" ]
672 tagrev="$tagobject"
673 scratch="${tagrev#????????????????}"
674 tagrev16="${tagrev%$scratch}"
675 read_tag_fields "$tagrev" || break
676 done
678 case "$peeledtype" in
679 commit)
680 # Only commit tags make sense to have rev-list operations
681 # performed on them
682 if [ -n "$prevtag" ]; then
683 # Show changes since the previous release
684 git shortlog "$prevtag..$newrev"
685 else
686 # No previous tag, show all the changes since time
687 # began
688 git shortlog $newrev
692 # XXX: Is there anything useful we can do for non-commit
693 # objects?
695 esac
697 echol $LOGEND
701 # Called for the deletion of an annotated tag
703 generate_delete_atag_email()
705 echol " was $oldrev"
706 echol ""
707 echol $LOGBEGIN
708 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
709 echol $LOGEND
712 # --------------- General references
715 # Called when any other type of reference is created (most likely a
716 # non-annotated tag)
718 generate_create_general_email()
720 echol " at $newrev ($newrev_type)"
722 generate_general_email
726 # Called when any other type of reference is updated (most likely a
727 # non-annotated tag)
729 generate_update_general_email()
731 echol " to $newrev ($newrev_type)"
732 echol " from $oldrev"
734 generate_general_email
738 # Called for creation or update of any other type of reference
740 generate_general_email()
742 # Unannotated tags are more about marking a point than releasing a
743 # version; therefore we don't do the shortlog summary that we do for
744 # annotated tags above - we simply show that the point has been
745 # marked, and print the log message for the marked point for
746 # reference purposes
748 # Note this section also catches any other reference type (although
749 # there aren't any) and deals with them in the same way.
751 echol ""
752 if [ "$newrev_type" = "commit" ]; then
753 echol $LOGBEGIN
754 git diff-tree --no-color --date=$datefmt --root -s --always --encoding=UTF-8 --format="$pfmt1$projurlesc/$newrev16$pfmt2" $newrev
755 echol $LOGEND
756 else
757 # What can we do here? The tag marks an object that is not
758 # a commit, so there is no log for us to display. It's
759 # probably not wise to output git cat-file as it could be a
760 # binary blob. We'll just say how big it is
761 echol "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
766 # Called for the deletion of any other type of reference
768 generate_delete_general_email()
770 echol " was $oldrev"
771 echol ""
772 echol $LOGBEGIN
773 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
774 echol $LOGEND
778 # --------------- Miscellaneous utilities
781 # Show new revisions as the user would like to see them in the email.
783 show_new_revisions()
785 # This shows all log entries that are not already covered by
786 # another ref - i.e. commits that are now accessible from this
787 # ref that were previously not accessible
788 # (see generate_update_branch_email for the explanation of this
789 # command)
791 # Revision range passed to rev-list differs for new vs. updated
792 # branches.
793 if [ "$change_type" = create ]
794 then
795 # Show all revisions exclusive to this (new) branch.
796 revspec=$newrev
797 else
798 # Branch update; show revisions not part of $oldrev.
799 revspec=$oldrev..$newrev
802 if [ "${MAIL_SH_OTHER_BRANCHES+set}" = "set" ]; then
803 case "$MAIL_SH_OTHER_BRANCHES" in
805 othertips="git cat-file blob '${MAIL_SH_OTHER_BRANCHES#@}' 2>/dev/null"
808 othertips='printf "%s\n" $MAIL_SH_OTHER_BRANCHES'
810 esac
811 else
812 othertips='git for-each-ref --format="%(refname)" refs/heads |
813 LC_ALL=C awk -v "refname=$refname" "\$1 != refname"'
815 eval "$othertips" |
816 git cat-file --batch-check='^%(objectname)' |
817 LC_ALL=C sed -e '/ missing$/d' |
818 # if [ -z "$custom_showrev" ]
819 # then
820 # git rev-list --pretty --stdin $revspec
821 # else
822 git --no-pager log --stdin --no-color --format=tformat:"%H %p" $revspec |
823 LC_ALL=C awk '{print $1 " " NF-1}' |
824 while read onerev pcnt
826 if [ -n "$custom_showrev" ]; then
827 eval $(printf "$custom_showrev" $onerev)
828 else
829 if [ ${summaryonly:-false} = false ]; then
830 if [ ${pcnt:-1} -gt 1 ]; then
831 opts="-p --cc"
832 else
833 opts="-p --stat=72 --summary"
835 else
836 if [ ${pcnt:-1} -gt 1 ]; then
837 opts="-s"
838 else
839 opts="--stat=72 --summary"
842 scratch="${onerev#????????????????}"
843 onerev16="${onerev%$scratch}"
844 git diff-tree --no-color --date=$datefmt $opts --always --encoding=UTF-8 --format="$pfmt1$projurlesc/$onerev16$pfmt2" --abbrev=$habbr -B -C --root $onerev
845 echo
847 done
848 # fi
852 size_limit()
854 size=0
855 while IFS= read -r line; do
856 # Include size of RFC 822 trailing CR+LF on each line
857 size=$(($size+${#line}+2))
858 if [ $size -gt $1 ]; then
859 echol "...e-mail trimmed, has been too large."
860 break
862 echol "$line"
863 done
867 sendmail_to_stdout()
873 send_mail()
875 if [ -n "$cfg_sender" ]; then
876 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
877 else
878 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
882 # ---------------------------- main()
884 # --- Constants
885 LOGBEGIN="- Log -----------------------------------------------------------------"
886 LOGEND="-----------------------------------------------------------------------"
888 # --- Config
889 . @basedir@/shlib.sh
891 # Git formatting to use
892 datefmt=rfc2822
893 habbr=12
895 # strftime formatting to use (empty is default rfc2822 format)
896 sdatefmt=
898 # This is --pretty=medium in two parts with a URL: line added
899 pfmt1='format:commit %H%nAuthor: %an <%ae>%nDate: %ad%nURL: <'
900 pfmt2='>%n%n%w(0,4,4)%s%n%n%b'
902 git_add_config core.abbrev=$habbr
904 # Set GIT_DIR either from the working directory, or from the environment
905 # variable.
906 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
907 if [ -z "$GIT_DIR" ]; then
908 echol >&2 "fatal: post-receive: GIT_DIR not set"
909 exit 1
912 projectdesc=
913 ! [ -s "$GIT_DIR/description" ] || projectdesc=$(LC_ALL=C sed -ne '1p' "$GIT_DIR/description")
914 # Check if the description is unchanged from it's default, and shorten it to
915 # a more manageable length if it is
916 if [ -z "$projectdesc" ] || LC_ALL=C expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
917 then
918 projectdesc="UNNAMED PROJECT"
921 # If --stdout is first argument send all output there instead of mailing
922 if [ "$1" = "--stdout" ]; then
923 shift
924 cfg_sendmail_bin="sendmail_to_stdout"
927 projectname="${4%.git}"
928 if [ -n "$projectname" ]; then
929 projectname="$projectname.git"
930 projectowner="$(config_get owner)"
931 else
932 projectname="$(basename "$PWD")"
934 projectboth="$projectname (\"$projectdesc\")"
935 projurl="$cfg_gitweburl/$projectname"
936 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
938 emailsender="$5"
939 emailextraheader="$6"
941 recipients=$(git config hooks.mailinglist)
942 summaryonly=$(git config --bool hooks.summaryonly 2>/dev/null || :)
943 announcerecipients=$(git config hooks.announcelist)
944 envelopesender=$(git config hooks.envelopesender)
945 emailprefix=$(git config hooks.emailprefix || echol "[$cfg_name] ")
946 custom_showrev=$(git config hooks.showrev)
948 # --- Main loop
949 # Allow dual mode: run from the command line just like the update hook, or
950 # if no arguments are given then run as a hook script
951 # If --stdout is first argument send all output there instead (handled above)
952 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
953 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
954 # Handle a single update rather than a batch on stdin
955 # Output will still be sent to sendmail unless --stdout is used
956 # Same 3 args as update hook (<refname> <old> <new>)
957 if prep_for_email $2 $3 $1; then
958 PAGER= generate_email | size_limit $((256*1024)) | send_mail
960 else
961 # MAIL_SH_OTHER_BRANCHES cannot possibly be valid for multiple updates
962 unset MAIL_SH_OTHER_BRANCHES
963 # Same input as pre-receive hook (each line is <old> <new> <refname>)
964 while read oldrev newrev refname
966 prep_for_email $oldrev $newrev $refname || continue
967 PAGER= generate_email | size_limit $((256*1024)) | send_mail
968 done
971 exit 0