ref updates: ignore non-update updates
[girocco.git] / taskd / mail.sh
blob4a10b0af09381d8a691e0ba2df8c8a80967cf386
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 # * Load shlib.
18 # * Default subject prefix is site name.
19 # * Unsubscribe instructions in email footer.
20 # * Default showrev includes gitweb link and show -C.
21 # * Nicer subject line.
22 # * Limit mail size to 256kb.
23 # =================
25 # This hook is stored in the contrib/hooks directory. Your distribution
26 # will have put this somewhere standard. You should make this script
27 # executable then link to it in the repository you would like to use it in.
28 # For example, on debian the hook is stored in
29 # /usr/share/git-core/contrib/hooks/post-receive-email:
31 # chmod a+x post-receive-email
32 # cd /path/to/your/repository.git
33 # ln -sf /usr/share/git-core/contrib/hooks/post-receive-email hooks/post-receive
35 # This hook script assumes it is enabled on the central repository of a
36 # project, with all users pushing only to it and not between each other. It
37 # will still work if you don't operate in that style, but it would become
38 # possible for the email to be from someone other than the person doing the
39 # push.
41 # Config
42 # ------
43 # hooks.mailinglist
44 # This is the list that all pushes will go to; leave it blank to not send
45 # emails for every ref update.
46 # hooks.summaryonly
47 # If true do not include the actual diff when showing new commits (the
48 # summary information will still be shown). Default is false.
49 # hooks.announcelist
50 # This is the list that all pushes of annotated tags will go to. Leave it
51 # blank to default to the mailinglist field. The announce emails lists
52 # the short log summary of the changes since the last annotated tag.
53 # hooks.envelopesender
54 # If set then the -f option is passed to sendmail to allow the envelope
55 # sender address to be set
56 # hooks.emailprefix
57 # All emails have their subjects prefixed with this prefix, or "[SCM]"
58 # if emailprefix is unset, to aid filtering
59 # hooks.showrev
60 # The shell command used to format each revision in the email, with
61 # "%s" replaced with the commit id. Defaults to "git rev-list -1
62 # --pretty %s", displaying the commit id, author, date and log
63 # message. To list full patches separated by a blank line, you
64 # could set this to "git show -C %s; echo".
65 # To list a gitweb/cgit URL *and* a full patch for each change set, use this:
66 # "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
67 # Be careful if "..." contains things that will be expanded by shell "eval"
68 # or printf.
70 # Notes
71 # -----
72 # All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
73 # "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
74 # give information for debugging.
77 # ---------------------------- Functions
80 # Function to output arguments without interpretation followed by \n
82 echol()
84 printf '%s\n' "$*"
88 # Function to read a tag's (the single argument) header fields into
89 # tagobject the "object" value
90 # tagtype the "type" value
91 # tagtag the "tag" value
92 # taggername from "tagger" value before first '<'
93 # taggerdate from "tagger" value after '>' but formatted into a date string
94 # taggeremail from "tagger" value between '<' and '>'
95 # On return all fields will be set (to empty if failure) and the three tagger
96 # fields will only be set to non-empty if a "tagger" header field is present.
97 # If the object does not exist or is not a tag the function returns failure.
98 read_tag_fields()
100 tagobject=
101 tagtype=
102 tagtag=
103 taggername=
104 taggerdate=
105 taggeremail=
106 if _tagfields="$(git cat-file tag "$1" | LC_ALL=C awk -F '[ ]' '
107 function quotesh(sv) {
108 gsub(/'\''/, "'\'\\\\\'\''", sv)
109 return "'\''" sv "'\''"
111 function trim(sv) {
112 sub(/^[ \t]+/, "", sv)
113 sub(/[ \t]+$/, "", sv)
114 return sv
116 function hval(sv) {
117 sub(/^[^ ]* /, "", sv)
118 return sv
120 NR==1,/^$/ {
121 if ($1 == "object") to=hval($0)
122 if ($1 == "type") ty=hval($0)
123 if ($1 == "tag") tt=hval($0)
124 if ($1 == "tagger") tr=hval($0)
126 END {
127 if (tt == "" || to == "" || ty == "") exit 1
128 print "tagobject=" quotesh(to)
129 print "tagtype=" quotesh(ty)
130 print "tagtag=" quotesh(tt)
131 if (match(tr, /^[ \t]*[^ \t<][^<]*/)) {
132 tn=substr(tr, RSTART, RLENGTH)
133 tr=substr(tr, RSTART + RLENGTH)
134 tn=trim(tn)
135 if (tn != "")
136 print "taggername=" quotesh(tn)
137 if (match(tr, /^<.*>/)) {
138 if (RLENGTH > 2)
139 te=substr(tr, RSTART+1, RLENGTH-2)
140 tr=substr(tr, RSTART + RLENGTH)
141 te=trim(te)
142 if (te != "")
143 print "taggeremail=" quotesh(te)
144 if (match(tr, /^[ \t]*[0-9]+([ \t]+[-+]?[0-9][0-9]([0-9][0-9]([0-9][0-9])?)?)[ \t]*$/))
145 td=trim(tr)
146 if (td != "")
147 print "taggerdate=" quotesh(td)
151 ')"; then
152 eval "$_tagfields"
153 [ -z "$taggerdate" ] || taggerdate="$(strftime "$sdatefmt" $taggerdate)"
154 return 0
156 return 1
160 # Function to prepare for email generation. This decides what type
161 # of update this is and whether an email should even be generated.
163 prep_for_email()
165 # --- Arguments
166 oldrev=$(git rev-parse --revs-only "$1" --)
167 newrev=$(git rev-parse --revs-only "$2" --)
168 [ "$oldrev" != "$newrev" ] || return 1
169 scratch="${newrev#????????????????}"
170 newrev16="${newrev%$scratch}"
171 refname="$3"
173 # --- Interpret
174 # 0000->1234 (create)
175 # 1234->2345 (update)
176 # 2345->0000 (delete)
177 if LC_ALL=C expr "$oldrev" : '0*$' >/dev/null
178 then
179 change_type="create"
180 else
181 if LC_ALL=C expr "$newrev" : '0*$' >/dev/null
182 then
183 change_type="delete"
184 else
185 change_type="update"
189 # --- Get the revision types
190 newrev_type=$(git cat-file -t $newrev 2> /dev/null)
191 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
192 case "$change_type" in
193 create|update)
194 rev="$newrev"
195 rev_type="$newrev_type"
197 delete)
198 rev="$oldrev"
199 rev_type="$oldrev_type"
201 esac
203 # The revision type tells us what type the commit is, combined with
204 # the location of the ref we can decide between
205 # - working branch
206 # - tracking branch
207 # - unannoted tag
208 # - annotated tag
209 case "$refname","$rev_type" in
210 refs/tags/*,commit)
211 # un-annotated tag
212 refname_type="tag"
213 short_refname=${refname##refs/tags/}
215 refs/tags/*,tag)
216 # annotated tag
217 refname_type="annotated tag"
218 short_refname=${refname##refs/tags/}
219 # change recipients
220 if [ -n "$announcerecipients" ]; then
221 recipients="$announcerecipients"
224 refs/heads/*,commit|refs/heads/*,tag)
225 # branch
226 refname_type="branch"
227 short_refname=${refname##refs/heads/}
229 refs/remotes/*,commit)
230 # tracking branch
231 refname_type="tracking branch"
232 short_refname=${refname##refs/remotes/}
233 echol >&2 "*** Push-update of tracking branch, $refname"
234 echol >&2 "*** - no email generated."
235 return 1
237 refs/mob/*,commit|refs/mob/*,tag)
238 # personal mob ref
239 refname_type="personal mob ref"
240 short_refname=${refname##refs/mob/}
241 echol >&2 "*** Push-update of personal mob ref, $refname"
242 echol >&2 "*** - no email generated."
243 return 1
246 # Anything else (is there anything else?)
247 echol >&2 "*** Unknown type of update to $refname ($rev_type)"
248 echol >&2 "*** - no email generated"
249 return 1
251 esac
253 # Check if we've got anyone to send to
254 if [ -z "$recipients" ]; then
255 case "$refname_type" in
256 "annotated tag")
257 config_name="hooks.announcelist"
260 config_name="hooks.mailinglist"
262 esac
263 echol >&2 "*** $config_name is not set so no email will be sent"
264 echol >&2 "*** for $refname update $oldrev->$newrev"
265 return 1
268 return 0
272 # Top level email generation function. This calls the appropriate
273 # body-generation routine after outputting the common header.
275 # Note this function doesn't actually generate any email output, that is
276 # taken care of by the functions it calls:
277 # - generate_email_header
278 # - generate_create_XXXX_email
279 # - generate_update_XXXX_email
280 # - generate_delete_XXXX_email
281 # - generate_email_footer
283 # Note also that this function cannot 'exit' from the script; when this
284 # function is running (in hook script mode), the send_mail() function
285 # is already executing in another process, connected via a pipe, and
286 # if this function exits without, whatever has been generated to that
287 # point will be sent as an email... even if nothing has been generated.
289 generate_email()
291 # Email parameters
292 # The email subject will contain the best description of the ref
293 # that we can build from the parameters
294 describe=$(git describe $rev 2>/dev/null)
295 if [ -z "$describe" ]; then
296 describe=$rev
299 generate_email_header
301 # Call the correct body generation function
302 fn_name=general
303 case "$refname_type" in
304 "tracking branch"|branch)
305 fn_name=branch
307 "annotated tag")
308 fn_name=atag
310 esac
311 generate_${change_type}_${fn_name}_email
313 generate_email_footer
316 generate_email_header()
318 # --- Email (all stdout will be the email)
319 # Generate header
320 if [ -n "$emailsender" ]; then
321 echol "From: $emailsender"
323 cat <<-EOF
324 To: $recipients
325 Subject: ${emailprefix}$projectname $refname_type $short_refname ${change_type}d: $describe
326 MIME-Version: 1.0
327 Content-Type: text/plain; charset=utf-8
328 Content-Transfer-Encoding: 8bit
330 [ -n "$cfg_suppress_x_girocco" ] || echol "X-Girocco: $cfg_gitweburl"
331 [ -z "$emailextraheader" ] || echol "$emailextraheader"
332 cat <<-EOF
333 X-Git-Refname: $refname
334 X-Git-Reftype: $refname_type
335 X-Git-Oldrev: $oldrev
336 X-Git-Newrev: $newrev
337 Auto-Submitted: auto-generated
339 This is an automated email from the git hooks/post-receive script. It was
340 generated because a ref change was pushed to the repository containing
341 the project $projectname.
343 The $refname_type, $short_refname has been ${change_type}d
347 generate_email_footer()
349 SPACE=" "
350 cat <<-EOF
353 $cfg_name automatic notification. Contact project admin $projectowner
354 if you want to unsubscribe, or site admin $cfg_admin if you receive
355 no reply.
356 --${SPACE}
357 $projectboth
361 # --------------- Branches
364 # Called for the creation of a branch
366 generate_create_branch_email()
368 # This is a new branch and so oldrev is not valid
369 echol " at $newrev ($newrev_type)"
370 echol ""
372 echol $LOGBEGIN
373 show_new_revisions
374 echol $LOGEND
376 # The diffstat is shown from an empty tree to the new revision.
377 # This is to show the truth of what happened in this change.
378 # Note that since Git 1.5.5 the empty tree object is ALWAYS available
379 # whether or not it's actually present in the repository.
380 echol ""
381 echol "Summary of changes:"
382 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder 4b825dc642cb6eb9a060e54bf8d69288fbee4904 $newrev
386 # Called for the change of a pre-existing branch
388 generate_update_branch_email()
390 # Consider this:
391 # 1 --- 2 --- O --- X --- 3 --- 4 --- N
393 # O is $oldrev for $refname
394 # N is $newrev for $refname
395 # X is a revision pointed to by some other ref, for which we may
396 # assume that an email has already been generated.
397 # In this case we want to issue an email containing only revisions
398 # 3, 4, and N. Given (almost) by
400 # git rev-list N ^O --not --all
402 # The reason for the "almost", is that the "--not --all" will take
403 # precedence over the "N", and effectively will translate to
405 # git rev-list N ^O ^X ^N
407 # So, we need to build up the list more carefully. git rev-parse
408 # will generate a list of revs that may be fed into git rev-list.
409 # We can get it to make the "--not --all" part and then filter out
410 # the "^N" with:
412 # git rev-parse --not --all | grep -v N
414 # Then, using the --stdin switch to git rev-list we have effectively
415 # manufactured
417 # git rev-list N ^O ^X
419 # This leaves a problem when someone else updates the repository
420 # while this script is running. Their new value of the ref we're
421 # working on would be included in the "--not --all" output; and as
422 # our $newrev would be an ancestor of that commit, it would exclude
423 # all of our commits. What we really want is to exclude the current
424 # value of $refname from the --not list, rather than N itself. So:
426 # git rev-parse --not --all | grep -v $(git rev-parse $refname)
428 # Get's us to something pretty safe (apart from the small time
429 # between refname being read, and git rev-parse running - for that,
430 # I give up)
433 # Next problem, consider this:
434 # * --- B --- * --- O ($oldrev)
436 # * --- X --- * --- N ($newrev)
438 # That is to say, there is no guarantee that oldrev is a strict
439 # subset of newrev (it would have required a --force, but that's
440 # allowed). So, we can't simply say rev-list $oldrev..$newrev.
441 # Instead we find the common base of the two revs and list from
442 # there.
444 # As above, we need to take into account the presence of X; if
445 # another branch is already in the repository and points at some of
446 # the revisions that we are about to output - we don't want them.
447 # The solution is as before: git rev-parse output filtered.
449 # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
451 # Tags pushed into the repository generate nice shortlog emails that
452 # summarise the commits between them and the previous tag. However,
453 # those emails don't include the full commit messages that we output
454 # for a branch update. Therefore we still want to output revisions
455 # that have been output on a tag email.
457 # Luckily, git rev-parse includes just the tool. Instead of using
458 # "--all" we use "--branches"; this has the added benefit that
459 # "remotes/" will be ignored as well.
461 # List all of the revisions that were removed by this update, in a
462 # fast-forward update, this list will be empty, because rev-list O
463 # ^N is empty. For a non-fast-forward, O ^N is the list of removed
464 # revisions
465 fast_forward=""
466 rev=""
467 for rev in $(git rev-list $newrev..$oldrev)
469 revtype=$(git cat-file -t "$rev")
470 echol " discards $rev ($revtype)"
471 done
472 if [ -z "$rev" ]; then
473 fast_forward=1
476 # List all the revisions from baserev to newrev in a kind of
477 # "table-of-contents"; note this list can include revisions that
478 # have already had notification emails and is present to show the
479 # full detail of the change from rolling back the old revision to
480 # the base revision and then forward to the new revision
481 for rev in $(git rev-list $oldrev..$newrev)
483 revtype=$(git cat-file -t "$rev")
484 echol " via $rev ($revtype)"
485 done
487 if [ "$fast_forward" ]; then
488 echol " from $oldrev ($oldrev_type)"
489 else
490 # 1. Existing revisions were removed. In this case newrev
491 # is a subset of oldrev - this is the reverse of a
492 # fast-forward, a rewind
493 # 2. New revisions were added on top of an old revision,
494 # this is a rewind and addition.
496 # (1) certainly happened, (2) possibly. When (2) hasn't
497 # happened, we set a flag to indicate that no log printout
498 # is required.
500 echol ""
502 # Find the common ancestor of the old and new revisions and
503 # compare it with newrev
504 baserev=$(git merge-base $oldrev $newrev)
505 rewind_only=""
506 if [ "$baserev" = "$newrev" ]; then
507 echol "This update discarded existing revisions and left the branch pointing at"
508 echol "a previous point in the repository history."
509 echol ""
510 echol " * -- * -- N ($newrev)"
511 echol " \\"
512 echol " O -- O -- O ($oldrev)"
513 echol ""
514 echol "The removed revisions are not necessarily gone - if another reference"
515 echol "still refers to them they will stay in the repository."
516 rewind_only=1
517 else
518 echol "This update added new revisions after undoing existing revisions. That is"
519 echol "to say, the old revision is not a strict subset of the new revision. This"
520 echol "situation occurs when you --force push a change and generate a repository"
521 echol "containing something like this:"
522 echol ""
523 echol " * -- * -- B -- O -- O -- O ($oldrev)"
524 echol " \\"
525 echol " N -- N -- N ($newrev)"
526 echol ""
527 echol "When this happens we assume that you've already had alert emails for all"
528 echol "of the O revisions, and so we here report only the revisions in the N"
529 echol "branch from the common base, B."
533 echol ""
534 if [ -z "$rewind_only" ]; then
535 echol "Those revisions listed above that are new to this repository have"
536 echol "not appeared on any other notification email; so we list those"
537 echol "revisions in full, below."
539 echol ""
540 echol $LOGBEGIN
541 show_new_revisions
543 # XXX: Need a way of detecting whether git rev-list actually
544 # outputted anything, so that we can issue a "no new
545 # revisions added by this update" message
547 echol $LOGEND
548 else
549 echol "No new revisions were added by this update."
552 # The diffstat is shown from the old revision to the new revision.
553 # This is to show the truth of what happened in this change.
554 # There's no point showing the stat from the base to the new
555 # revision because the base is effectively a random revision at this
556 # point - the user will be interested in what this revision changed
557 # - including the undoing of previous revisions in the case of
558 # non-fast-forward updates.
559 echol ""
560 echol "Summary of changes:"
561 git diff-tree --no-color --stat=72 --summary -B --find-copies-harder $oldrev..$newrev
565 # Called for the deletion of a branch
567 generate_delete_branch_email()
569 echol " was $oldrev"
570 echol ""
571 echol $LOGBEGIN
572 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
573 echol $LOGEND
576 # --------------- Annotated tags
579 # Called for the creation of an annotated tag
581 generate_create_atag_email()
583 echol " at $newrev ($newrev_type)"
585 generate_atag_email
589 # Called for the update of an annotated tag (this is probably a rare event
590 # and may not even be allowed)
592 generate_update_atag_email()
594 echol " to $newrev ($newrev_type)"
595 echol " from $oldrev (which is now obsolete)"
597 generate_atag_email
601 # Called when an annotated tag is created or changed
603 generate_atag_email()
605 # Use read_tag_fields to pull out the individual fields from the
606 # tag and git cat-file --batch-check to fully peel the tag if needed
607 tagrev="$newrev"
608 tagrev16="$newrev16"
609 read_tag_fields "$tagrev"
610 if [ "$tagtype" = "tag" ]; then
611 # fully peel the tag
612 peeledobject=
613 peeledtype=
614 info="$(echo "$tagrev^{}" | git cat-file --batch-check='
615 peeledobject=%(objectname)
616 peeledtype=%(objecttype)'
618 case "$info" in *type=*) eval "$info";; *)
619 peeledtype=missing
620 esac
621 else
622 peeledobject="$tagobject"
623 peeledtype="$tagtype"
626 echol " tagging $peeledobject ($peeledtype)"
627 case "$peeledtype" in
628 commit)
630 # If the tagged object is a commit, then we assume this is a
631 # release, and so we calculate which tag this tag is
632 # replacing
633 prevtag=$(git describe --abbrev=0 "$peeledobject^" 2>/dev/null)
635 if [ -n "$prevtag" ]; then
636 echol " replaces $prevtag"
640 echol " length $(git cat-file -s "$peeledobject" 2>/dev/null) bytes"
642 esac
643 echol " tagged by $taggername"
644 echol " on $taggerdate"
646 echol ""
647 echol $LOGBEGIN
649 while
650 echol "tag $tagrev"
651 echol "Tag: $tagtag"
652 echol "Object: $tagobject ($tagtype)"
653 [ -z "$taggername" ] || \
654 echol "Tagger: $taggername"
655 [ -z "$taggerdate" ] || \
656 echol "Date: $taggerdate"
657 echol "URL: <$projurl/$tagrev16>"
658 echol ""
660 # Show the content of the tag message; this might contain a change
661 # log or release notes so is worth displaying.
662 git cat-file tag "$tagrev" | LC_ALL=C sed -e '1,/^$/d;s/^/ /;'
663 echol ""
664 [ "$tagtype" = "tag" ]
666 tagrev="$tagobject"
667 scratch="${tagrev#????????????????}"
668 tagrev16="${tagrev%$scratch}"
669 read_tag_fields "$tagrev" || break
670 done
672 case "$peeledtype" in
673 commit)
674 # Only commit tags make sense to have rev-list operations
675 # performed on them
676 if [ -n "$prevtag" ]; then
677 # Show changes since the previous release
678 git shortlog "$prevtag..$newrev"
679 else
680 # No previous tag, show all the changes since time
681 # began
682 git shortlog $newrev
686 # XXX: Is there anything useful we can do for non-commit
687 # objects?
689 esac
691 echol $LOGEND
695 # Called for the deletion of an annotated tag
697 generate_delete_atag_email()
699 echol " was $oldrev"
700 echol ""
701 echol $LOGBEGIN
702 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
703 echol $LOGEND
706 # --------------- General references
709 # Called when any other type of reference is created (most likely a
710 # non-annotated tag)
712 generate_create_general_email()
714 echol " at $newrev ($newrev_type)"
716 generate_general_email
720 # Called when any other type of reference is updated (most likely a
721 # non-annotated tag)
723 generate_update_general_email()
725 echol " to $newrev ($newrev_type)"
726 echol " from $oldrev"
728 generate_general_email
732 # Called for creation or update of any other type of reference
734 generate_general_email()
736 # Unannotated tags are more about marking a point than releasing a
737 # version; therefore we don't do the shortlog summary that we do for
738 # annotated tags above - we simply show that the point has been
739 # marked, and print the log message for the marked point for
740 # reference purposes
742 # Note this section also catches any other reference type (although
743 # there aren't any) and deals with them in the same way.
745 echol ""
746 if [ "$newrev_type" = "commit" ]; then
747 echol $LOGBEGIN
748 git diff-tree --no-color --date=$datefmt --root -s --always --encoding=UTF-8 --format="$pfmt1$projurlesc/$newrev16$pfmt2" $newrev
749 echol $LOGEND
750 else
751 # What can we do here? The tag marks an object that is not
752 # a commit, so there is no log for us to display. It's
753 # probably not wise to output git cat-file as it could be a
754 # binary blob. We'll just say how big it is
755 echol "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
760 # Called for the deletion of any other type of reference
762 generate_delete_general_email()
764 echol " was $oldrev"
765 echol ""
766 echol $LOGBEGIN
767 git diff-tree --no-color --date=$datefmt -s --abbrev-commit --abbrev=$habbr --always --encoding=UTF-8 --pretty=oneline $oldrev
768 echol $LOGEND
772 # --------------- Miscellaneous utilities
775 # Show new revisions as the user would like to see them in the email.
777 show_new_revisions()
779 # This shows all log entries that are not already covered by
780 # another ref - i.e. commits that are now accessible from this
781 # ref that were previously not accessible
782 # (see generate_update_branch_email for the explanation of this
783 # command)
785 # Revision range passed to rev-list differs for new vs. updated
786 # branches.
787 if [ "$change_type" = create ]
788 then
789 # Show all revisions exclusive to this (new) branch.
790 revspec=$newrev
791 else
792 # Branch update; show revisions not part of $oldrev.
793 revspec=$oldrev..$newrev
796 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
797 LC_ALL=C grep -F -v $refname)
798 git rev-parse --not $other_branches |
799 # if [ -z "$custom_showrev" ]
800 # then
801 # git rev-list --pretty --stdin $revspec
802 # else
803 git --no-pager log --stdin --no-color --format=tformat:"%H %p" $revspec |
804 LC_ALL=C awk '{print $1 " " NF-1}' |
805 while read onerev pcnt
807 if [ -n "$custom_showrev" ]; then
808 eval $(printf "$custom_showrev" $onerev)
809 else
810 if [ ${summaryonly:-false} = false ]; then
811 if [ ${pcnt:-1} -gt 1 ]; then
812 opts="-p --cc"
813 else
814 opts="-p --stat=72 --summary"
816 else
817 if [ ${pcnt:-1} -gt 1 ]; then
818 opts="-s"
819 else
820 opts="--stat=72 --summary"
823 scratch="${onerev#????????????????}"
824 onerev16="${onerev%$scratch}"
825 git diff-tree --no-color --date=$datefmt $opts --always --encoding=UTF-8 --format="$pfmt1$projurlesc/$onerev16$pfmt2" --abbrev=$habbr -B -C --root $onerev
826 echo
828 done
829 # fi
833 size_limit()
835 size=0
836 while IFS= read -r line; do
837 # Include size of RFC 822 trailing CR+LF on each line
838 size=$(($size+${#line}+2))
839 if [ $size -gt $1 ]; then
840 echol "...e-mail trimmed, has been too large."
841 break
843 echol "$line"
844 done
848 sendmail_to_stdout()
854 send_mail()
856 if [ -n "$cfg_sender" ]; then
857 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t -f "$cfg_sender"
858 else
859 "${cfg_sendmail_bin:-/usr/sbin/sendmail}" -i -t
863 # ---------------------------- main()
865 # --- Constants
866 LOGBEGIN="- Log -----------------------------------------------------------------"
867 LOGEND="-----------------------------------------------------------------------"
869 # --- Config
870 . @basedir@/shlib.sh
872 # Git formatting to use
873 datefmt=rfc2822
874 habbr=12
876 # strftime formatting to use (empty is default rfc2822 format)
877 sdatefmt=
879 # This is --pretty=medium in two parts with a URL: line added
880 pfmt1='format:commit %H%nAuthor: %an <%ae>%nDate: %ad%nURL: <'
881 pfmt2='>%n%n%w(0,4,4)%s%n%n%b'
883 git_add_config core.abbrev=$habbr
885 # Set GIT_DIR either from the working directory, or from the environment
886 # variable.
887 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
888 if [ -z "$GIT_DIR" ]; then
889 echol >&2 "fatal: post-receive: GIT_DIR not set"
890 exit 1
893 projectdesc=
894 ! [ -s "$GIT_DIR/description" ] || projectdesc=$(LC_ALL=C sed -ne '1p' "$GIT_DIR/description")
895 # Check if the description is unchanged from it's default, and shorten it to
896 # a more manageable length if it is
897 if [ -z "$projectdesc" ] || LC_ALL=C expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
898 then
899 projectdesc="UNNAMED PROJECT"
902 # If --stdout is first argument send all output there instead of mailing
903 if [ "$1" = "--stdout" ]; then
904 shift
905 cfg_sendmail_bin="sendmail_to_stdout"
908 projectname="${4%.git}"
909 if [ -n "$projectname" ]; then
910 projectname="$projectname.git"
911 projectowner="$(config_get owner)"
912 else
913 projectname="$(basename "$PWD")"
915 projectboth="$projectname (\"$projectdesc\")"
916 projurl="$cfg_gitweburl/$projectname"
917 projurlesc="$(printf '%s\n' "$projurl" | sed -e 's/%/%%/g')"
919 emailsender="$5"
920 emailextraheader="$6"
922 recipients=$(git config hooks.mailinglist)
923 summaryonly=$(git config --bool hooks.summaryonly 2>/dev/null || :)
924 announcerecipients=$(git config hooks.announcelist)
925 envelopesender=$(git config hooks.envelopesender)
926 emailprefix=$(git config hooks.emailprefix || echol "[$cfg_name] ")
927 custom_showrev=$(git config hooks.showrev)
929 # --- Main loop
930 # Allow dual mode: run from the command line just like the update hook, or
931 # if no arguments are given then run as a hook script
932 # If --stdout is first argument send all output there instead (handled above)
933 # Optional 4th (projectname), 5th (sender) and 6th (extra header) arguments
934 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
935 # Handle a single update rather than a batch on stdin
936 # Output will still be sent to sendmail unless --stdout is used
937 # Same 3 args as update hook (<refname> <old> <new>)
938 if prep_for_email $2 $3 $1; then
939 PAGER= generate_email | size_limit $((256*1024)) | send_mail
941 else
942 # Same input as pre-receive hook (each line is <old> <new> <refname>)
943 while read oldrev newrev refname
945 prep_for_email $oldrev $newrev $refname || continue
946 PAGER= generate_email | size_limit $((256*1024)) | send_mail
947 done
950 exit 0