7171# ---------------------------- Functions
7272
7373#
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.
7776#
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 ()
8778{
8879 # --- Arguments
8980 oldrev=$( git rev-parse $1 )
@@ -159,7 +150,7 @@ generate_email()
159150 # Anything else (is there anything else?)
160151 echo >&2 " *** Unknown type of update to $refname ($rev_type )"
161152 echo >&2 " *** - no email generated"
162- exit 1
153+ return 0
163154 ;;
164155 esac
165156
@@ -175,9 +166,32 @@ generate_email()
175166 esac
176167 echo >&2 " *** $config_name is not set so no email will be sent"
177168 echo >&2 " *** for $refname update $oldrev ->$newrev "
178- exit 0
169+ return 0
179170 fi
180171
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+ {
181195 # Email parameters
182196 # The email subject will contain the best description of the ref
183197 # that we can build from the parameters
@@ -717,10 +731,11 @@ if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
717731 # Output to the terminal in command line mode - if someone wanted to
718732 # resend an email; they could redirect the output to sendmail
719733 # themselves
720- PAGER= generate_email $2 $3 $1
734+ prep_for_email $2 $3 $1 && PAGER= generate_email
721735else
722736 while read oldrev newrev refname
723737 do
724- generate_email $oldrev $newrev $refname $maxlines | send_mail
738+ prep_for_email $oldrev $newrev $refname || continue
739+ generate_email $maxlines | send_mail
725740 done
726741fi
0 commit comments