post-receive-email: ensure sent messages are not empty
Changes the logic in the script to determine whether an email message will be sent before invoking the send_mail() function; otherwise, if the logic determines that a message will not be sent, send_mail() will cause an empty email to be sent. In addition, ensures that if multiple refs are updated and a message cannot be sent for one of them, the others are still processed normally. Signed-off-by: Kevin P. Fleming <kpfleming@digium.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Kevin P. Fleming committed
Sep 10, 2010 at 11:09 UTC
53cad69105e524deb014fe2ddec1d5804636d182
1 file changed
+31
-16
contrib/hooks/post-receive-email
+31
-16
@@ -71,19 +71,10 @@
71
# ---------------------------- Functions
72
73
#
74
-# Top level email generation function. This decides what type of update
75
-# this is and calls the appropriate body-generation routine after outputting
76
-# the common header
74
+# Function to prepare for email generation. This decides what type
75
+# of update this is and whether an email should even be generated.
76
#
78
-# Note this function doesn't actually generate any email output, that is
79
-# taken care of by the functions it calls:
80
-# - generate_email_header
81
-# - generate_create_XXXX_email
82
-# - generate_update_XXXX_email
83
-# - generate_delete_XXXX_email
84
-# - generate_email_footer
85
-#
86
-generate_email()
77
+prep_for_email()
78
{
79
# --- Arguments
80
oldrev=$(git rev-parse $1)
@@ -159,7 +150,7 @@ generate_email()
150
# Anything else (is there anything else?)
151
echo >&2 "*** Unknown type of update to $refname ($rev_type)"
152
echo >&2 "*** - no email generated"
162
- exit 1
153
+ return 0
154
;;
155
esac
156
@@ -175,9 +166,32 @@ generate_email()
166
esac
167
echo >&2 "*** $config_name is not set so no email will be sent"
168
echo >&2 "*** for $refname update $oldrev->$newrev"
178
- exit 0
169
+ return 0
170
fi
171
172
+ return 1
173
+}
174
+
175
+#
176
+# Top level email generation function. This calls the appropriate
177
+# body-generation routine after outputting the common header.
178
+#
179
+# Note this function doesn't actually generate any email output, that is
180
+# taken care of by the functions it calls:
181
+# - generate_email_header
182
+# - generate_create_XXXX_email
183
+# - generate_update_XXXX_email
184
+# - generate_delete_XXXX_email
185
+# - generate_email_footer
186
+#
187
+# Note also that this function cannot 'exit' from the script; when this
188
+# function is running (in hook script mode), the send_mail() function
189
+# is already executing in another process, connected via a pipe, and
190
+# if this function exits without, whatever has been generated to that
191
+# point will be sent as an email... even if nothing has been generated.
192
+#
193
+generate_email()
194
+{
195
# Email parameters
196
# The email subject will contain the best description of the ref
197
# that we can build from the parameters
@@ -717,10 +731,11 @@ if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
731
# Output to the terminal in command line mode - if someone wanted to
732
# resend an email; they could redirect the output to sendmail
733
# themselves
720
- PAGER= generate_email $2 $3 $1
734
+ prep_for_email $2 $3 $1 && PAGER= generate_email
735
else
736
while read oldrev newrev refname
737
do
724
- generate_email $oldrev $newrev $refname $maxlines | send_mail
738
+ prep_for_email $oldrev $newrev $refname || continue
739
+ generate_email $maxlines | send_mail
740
done
741
fi