From 39c2cd3c7c3df80a9d7c1850e4fd4e53fb5fc2b1 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 17 Jan 2014 03:57:34 -0800 Subject: [PATCH] mail.sh: apply more recent updates from contrib/hooks/post-receive-email Since mail.sh moved into Girocco, it stopped automatically picking up the contrib/hooks/post-receive-email updates. Apply the ones that are most applicable and do not conflict with any of the Girocco-specific changes. These are: * Add mime headers and make sure the body is UTF-8 * Use plumbing commands instead of porcelain to decrease fragility * Add Auto-Submitted header (RFC3834) to help quell vacation auto responders * Match up $LOGBEGIN..$LOGEND pairs correctly * Fix some spelling errors and comments * Update the logic that avoids sending empty emails * In stdin mode allow subsequent emails to be sent even if earlier ones fail There should be no change in content of the sent emails (other than making sure they're now encoded in UTF-8). --- taskd/mail.sh | 99 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/taskd/mail.sh b/taskd/mail.sh index 38512f2..957b980 100755 --- a/taskd/mail.sh +++ b/taskd/mail.sh @@ -26,11 +26,11 @@ # will have put this somewhere standard. You should make this script # executable then link to it in the repository you would like to use it in. # For example, on debian the hook is stored in -# /usr/share/doc/git-core/contrib/hooks/post-receive-email: +# /usr/share/git-core/contrib/hooks/post-receive-email: # # chmod a+x post-receive-email # cd /path/to/your/repository.git -# ln -sf /usr/share/doc/git-core/contrib/hooks/post-receive-email hooks/post-receive +# ln -sf /usr/share/git-core/contrib/hooks/post-receive-email hooks/post-receive # # This hook script assumes it is enabled on the central repository of a # project, with all users pushing only to it and not between each other. It @@ -74,19 +74,10 @@ # ---------------------------- Functions # -# Top level email generation function. This decides what type of update -# this is and calls the appropriate body-generation routine after outputting -# the common header +# Function to prepare for email generation. This decides what type +# of update this is and whether an email should even be generated. # -# Note this function doesn't actually generate any email output, that is -# taken care of by the functions it calls: -# - generate_email_header -# - generate_create_XXXX_email -# - generate_update_XXXX_email -# - generate_delete_XXXX_email -# - generate_email_footer -# -generate_email() +prep_for_email() { # --- Arguments oldrev=$(git rev-parse $1) @@ -156,7 +147,7 @@ generate_email() echo >&2 "*** Push-update of tracking branch, $refname" echo >&2 "*** - no email generated." echo "" - exit 0 + return 1 ;; refs/mob/*,commit|refs/mob/*,tag) # personal mob ref @@ -165,13 +156,14 @@ generate_email() echo >&2 "*** Push-update of personal mob ref, $refname" echo >&2 "*** - no email generated." echo "" - exit 0 + return 1 ;; *) # Anything else (is there anything else?) echo >&2 "*** Unknown type of update to $refname ($rev_type)" echo >&2 "*** - no email generated" - exit 1 + echo "" + return 1 ;; esac @@ -188,9 +180,32 @@ generate_email() echo >&2 "*** $config_name is not set so no email will be sent" echo >&2 "*** for $refname update $oldrev->$newrev" echo "" - exit 0 + return 1 fi + return 0 +} + +# +# Top level email generation function. This calls the appropriate +# body-generation routine after outputting the common header. +# +# Note this function doesn't actually generate any email output, that is +# taken care of by the functions it calls: +# - generate_email_header +# - generate_create_XXXX_email +# - generate_update_XXXX_email +# - generate_delete_XXXX_email +# - generate_email_footer +# +# Note also that this function cannot 'exit' from the script; when this +# function is running (in hook script mode), the send_mail() function +# is already executing in another process, connected via a pipe, and +# if this function exits without, whatever has been generated to that +# point will be sent as an email... even if nothing has been generated. +# +generate_email() +{ # Email parameters # The email subject will contain the best description of the ref # that we can build from the parameters @@ -226,6 +241,9 @@ generate_email_header() cat <<-EOF To: $recipients Subject: ${emailprefix}$projectname $refname_type $short_refname ${change_type}d: $describe + MIME-Version: 1.0 + Content-Type: text/plain; charset=utf-8 + Content-Transfer-Encoding: 8bit EOF [ -z "$emailextraheader" ] || echo "$emailextraheader" cat <<-EOF @@ -233,6 +251,7 @@ generate_email_header() X-Git-Reftype: $refname_type X-Git-Oldrev: $oldrev X-Git-Newrev: $newrev + Auto-Submitted: auto-generated This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing @@ -349,8 +368,8 @@ generate_update_branch_email() # "remotes/" will be ignored as well. # List all of the revisions that were removed by this update, in a - # fast forward update, this list will be empty, because rev-list O - # ^N is empty. For a non fast forward, O ^N is the list of removed + # fast-forward update, this list will be empty, because rev-list O + # ^N is empty. For a non-fast-forward, O ^N is the list of removed # revisions fast_forward="" rev="" @@ -401,7 +420,7 @@ generate_update_branch_email() echo " \\" echo " O -- O -- O ($oldrev)" echo "" - echo "The removed revisions are not necessarilly gone - if another reference" + echo "The removed revisions are not necessarily gone - if another reference" echo "still refers to them they will stay in the repository." rewind_only=1 else @@ -445,10 +464,10 @@ generate_update_branch_email() # revision because the base is effectively a random revision at this # point - the user will be interested in what this revision changed # - including the undoing of previous revisions in the case of - # non-fast forward updates. + # non-fast-forward updates. echo "" echo "Summary of changes:" - git diff-tree --stat --summary --find-copies-harder $oldrev..$newrev + git diff-tree --no-color --stat --summary --find-copies-harder $oldrev..$newrev } # @@ -458,8 +477,8 @@ generate_delete_branch_email() { echo " was $oldrev" echo "" - echo $LOGEND - git show -s --pretty=oneline $oldrev + echo $LOGBEGIN + git diff-tree --no-color -s --always --encoding=UTF-8 --pretty=oneline $oldrev echo $LOGEND } @@ -535,11 +554,11 @@ generate_atag_email() # performed on them if [ -n "$prevtag" ]; then # Show changes since the previous release - git rev-list --pretty=short "$prevtag..$newrev" | git shortlog + git shortlog "$prevtag..$newrev" else # No previous tag, show all the changes since time # began - git rev-list --pretty=short $newrev | git shortlog + git shortlog $newrev fi ;; *) @@ -558,8 +577,8 @@ generate_delete_atag_email() { echo " was $oldrev" echo "" - echo $LOGEND - git show -s --pretty=oneline $oldrev + echo $LOGBEGIN + git diff-tree --no-color -s --always --encoding=UTF-8 --pretty=oneline $oldrev echo $LOGEND } @@ -605,7 +624,7 @@ generate_general_email() echo "" if [ "$newrev_type" = "commit" ]; then echo $LOGBEGIN - git show --no-color --root -s --pretty=medium $newrev + git diff-tree --no-color --root -s --always --encoding=UTF-8 --pretty=medium $newrev echo $LOGEND else # What can we do here? The tag marks an object that is not @@ -623,8 +642,8 @@ generate_delete_general_email() { echo " was $oldrev" echo "" - echo $LOGEND - git show -s --pretty=oneline $oldrev + echo $LOGBEGIN + git diff-tree --no-color -s --always --encoding=UTF-8 --pretty=oneline $oldrev echo $LOGEND } @@ -668,7 +687,7 @@ show_new_revisions() else echo "$cfg_gitweburl/$projectname/commit/$onerev" echo - git show -C $onerev + git diff-tree --no-color -p --always --encoding=UTF-8 --pretty=medium -C $onerev echo fi done @@ -692,13 +711,6 @@ size_limit() send_mail() { - if ! IFS= read header; then - exit 1 - fi - if [ -z "$header" ]; then - exit 0 - fi - { echo "$header"; cat; } | if [ -n "$envelopesender" ]; then /usr/sbin/sendmail -t -f "$envelopesender" else @@ -757,10 +769,13 @@ if [ -n "$1" -a -n "$2" -a -n "$3" ]; then # Output to the terminal in command line mode - if someone wanted to # resend an email; they could redirect the output to sendmail # themselves - generate_email $2 $3 $1 | size_limit $((256*1024)) | send_mail + if prep_for_email $2 $3 $1; then + PAGER= generate_email | size_limit $((256*1024)) | send_mail + fi else while read oldrev newrev refname do - generate_email $oldrev $newrev $refname | size_limit $((256*1024)) | send_mail + prep_for_email $oldrev $newrev $refname || continue + PAGER= generate_email | size_limit $((256*1024)) | send_mail done fi -- 2.11.4.GIT