{"thread":{"id":"25067","subject":"[PATCH] post-receive-email: ensure sent messages are not empty","startedAt":"2010-09-10T16:09:39Z","lastAt":"2010-09-10T16:09:39Z","messageCount":1,"participants":["Kevin P. Fleming"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"150444","messageId":"1284134979-31376-1-git-send-email-kpfleming@digium.com","threadId":"25067","inReplyTo":null,"subject":"[PATCH] post-receive-email: ensure sent messages are not empty","fromName":"Kevin P. Fleming","fromEmail":"kpfleming@digium.com","sentAt":"2010-09-10T16:09:39Z","receivedAt":"2010-09-10T16:09:39Z","isPatch":true,"sender":{"key":"kpfleming@digium.com","avatar":null},"body":"Changes the logic in the script to determine whether an email message\nwill be sent before invoking the send_mail() function; otherwise, if\nthe logic determines that a message will not be sent, send_mail() will\ncause an empty email to be sent. In addition, ensures that if multiple\nrefs are updated and a message cannot be sent for one of them,\nthe others are still processed normally.\n\nSigned-off-by: Kevin P. Fleming <kpfleming@digium.com>\n---\n contrib/hooks/post-receive-email |   47 +++++++++++++++++++++++++-------------\n 1 files changed, 31 insertions(+), 16 deletions(-)\n\ndiff --git a/contrib/hooks/post-receive-email b/contrib/hooks/post-receive-email\nindex 0085086..85724bf 100755\n--- a/contrib/hooks/post-receive-email\n+++ b/contrib/hooks/post-receive-email\n@@ -71,19 +71,10 @@\n # ---------------------------- Functions\n \n #\n-# Top level email generation function.  This decides what type of update\n-# this is and calls the appropriate body-generation routine after outputting\n-# the common header\n+# Function to prepare for email generation. This decides what type\n+# of update this is and whether an email should even be generated.\n #\n-# Note this function doesn't actually generate any email output, that is\n-# taken care of by the functions it calls:\n-#  - generate_email_header\n-#  - generate_create_XXXX_email\n-#  - generate_update_XXXX_email\n-#  - generate_delete_XXXX_email\n-#  - generate_email_footer\n-#\n-generate_email()\n+prep_for_email()\n {\n \t# --- Arguments\n \toldrev=$(git rev-parse $1)\n@@ -159,7 +150,7 @@ generate_email()\n \t\t\t# Anything else (is there anything else?)\n \t\t\techo >&2 \"*** Unknown type of update to $refname ($rev_type)\"\n \t\t\techo >&2 \"***  - no email generated\"\n-\t\t\texit 1\n+\t\t\treturn 0\n \t\t\t;;\n \tesac\n \n@@ -175,9 +166,32 @@ generate_email()\n \t\tesac\n \t\techo >&2 \"*** $config_name is not set so no email will be sent\"\n \t\techo >&2 \"*** for $refname update $oldrev->$newrev\"\n-\t\texit 0\n+\t\treturn 0\n \tfi\n \n+\treturn 1\n+}\n+\n+#\n+# Top level email generation function.  This calls the appropriate\n+# body-generation routine after outputting the common header.\n+#\n+# Note this function doesn't actually generate any email output, that is\n+# taken care of by the functions it calls:\n+#  - generate_email_header\n+#  - generate_create_XXXX_email\n+#  - generate_update_XXXX_email\n+#  - generate_delete_XXXX_email\n+#  - generate_email_footer\n+#\n+# Note also that this function cannot 'exit' from the script; when this\n+# function is running (in hook script mode), the send_mail() function\n+# is already executing in another process, connected via a pipe, and\n+# if this function exits without, whatever has been generated to that\n+# point will be sent as an email... even if nothing has been generated.\n+#\n+generate_email()\n+{\n \t# Email parameters\n \t# The email subject will contain the best description of the ref\n \t# that we can build from the parameters\n@@ -717,10 +731,11 @@ if [ -n \"$1\" -a -n \"$2\" -a -n \"$3\" ]; then\n \t# Output to the terminal in command line mode - if someone wanted to\n \t# resend an email; they could redirect the output to sendmail\n \t# themselves\n-\tPAGER= generate_email $2 $3 $1\n+\tprep_for_email $2 $3 $1 && PAGER= generate_email\n else\n \twhile read oldrev newrev refname\n \tdo\n-\t\tgenerate_email $oldrev $newrev $refname $maxlines | send_mail\n+\t\tprep_for_email $oldrev $newrev $refname || continue\n+\t\tgenerate_email $maxlines | send_mail\n \tdone\n fi\n-- \n1.7.2.2\n"}]}