# [PATCH] post-receive-email: do not call sendmail if no mail was generated

3 messages from 2009-08-28 to 2009-09-08. Participants: Lars Noschinski, Junio C Hamano.
Thread: https://gitlist.dev/t/20773

## Lars Noschinski, 2009-08-28 17:39

Subject: [PATCH] post-receive-email: do not call sendmail if no mail was generated
Message-ID: <1251481187-6361-1-git-send-email-lars@public.noschinski.de>
URL: https://gitlist.dev/e/1251481187-6361-1-git-send-email-lars%40public.noschinski.de

```
contrib/hooks/post-receive-email used to call the send_mail function
(and thus, /usr/sbin/sendmail), even if generate_mail returned an error.
This is problematic, as the sendmail binary provided by exim4 generates
an error mail if provided with an empty input.

Therefore, this commit changes post-receive-email to only call sendmail
if generate_mail returned without error.

Signed-off-by: Lars Noschinski <lars@public.noschinski.de>
---
 contrib/hooks/post-receive-email |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

Obvious drawback of this solution is that the mails are kept in memory as a
whole. But the mails should be small enough, so that this does not hurt.

I'm not sure how to write a test for this bug, as the hook (correctly) uses an
absolute path to call sendmail.

diff --git a/contrib/hooks/post-receive-email b/contrib/hooks/post-receive-email
index 2a66063..818a270 100755
--- a/contrib/hooks/post-receive-email
+++ b/contrib/hooks/post-receive-email
@@ -684,6 +684,9 @@ if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
 else
 	while read oldrev newrev refname
 	do
-		generate_email $oldrev $newrev $refname | send_mail
+		mail="$(generate_email $oldrev $newrev $refname)"
+		if [ $? -eq 0 ]; then
+			printf '%s' "$mail" | send_mail
+		fi
 	done
 fi
-- 
1.6.3.3

```

## Lars Noschinski, 2009-09-08 09:20

Subject: Re: [PATCH] post-receive-email: do not call sendmail if no mail was generated
Message-ID: <20090908092059.GA8207@lars.home.noschinski.de>
URL: https://gitlist.dev/e/20090908092059.GA8207%40lars.home.noschinski.de
In-Reply-To: <1251481187-6361-1-git-send-email-lars@public.noschinski.de>

```
* Lars Noschinski <lars@public.noschinski.de> [09-08-28 19:39]:
> contrib/hooks/post-receive-email used to call the send_mail function
> (and thus, /usr/sbin/sendmail), even if generate_mail returned an error.
> This is problematic, as the sendmail binary provided by exim4 generates
> an error mail if provided with an empty input.
> 
> Therefore, this commit changes post-receive-email to only call sendmail
> if generate_mail returned without error.
> 
> Signed-off-by: Lars Noschinski <lars@public.noschinski.de>

Is anything wrong with this patch? Or is it just queued to be committed
some time?

 - Lars.

```

## Junio C Hamano, 2009-09-08 16:57

Subject: Re: [PATCH] post-receive-email: do not call sendmail if no mail was generated
Message-ID: <7vk5098jcv.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vk5098jcv.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20090908092059.GA8207@lars.home.noschinski.de>

```
Lars Noschinski <lars@public.noschinski.de> writes:

> * Lars Noschinski <lars@public.noschinski.de> [09-08-28 19:39]:
>> contrib/hooks/post-receive-email used to call the send_mail function
>> (and thus, /usr/sbin/sendmail), even if generate_mail returned an error.
>> This is problematic, as the sendmail binary provided by exim4 generates
>> an error mail if provided with an empty input.
>> 
>> Therefore, this commit changes post-receive-email to only call sendmail
>> if generate_mail returned without error.
>> 
>> Signed-off-by: Lars Noschinski <lars@public.noschinski.de>
>
> Is anything wrong with this patch? Or is it just queued to be committed
> some time?

It is not queued anywhere as far as I am concerned.

I was waiting for others to review the patch and nothing happened, so the
patch was in limbo.  Thanks for sending a reminder message I am responding
to.  You did the right thing when nothing happened to a patch that did not
see any discussion.

You can avoid this by CC'ing people who have been involved in the past
with the parts of the system you are patching in the initial posting of
your patch (I am not one of them, so CC'ing me didn't help).

Here are my knee-jerk reactions to the patch after a quick glance, without
thinking deeply nor looking at the other parts of the file you did not
touch, but looking only at the parts shown in your patch:

 - Slurping generate_email's output into a shell variable is a bad taste.
   You said that the message is always small enough but _how_ do we know
   it?

 - If this is to save us from a quirk in some but not all implementations
   of /usr/lib/sendmail, then shouldn't the logic be made into a new
   conditional?

 - I do not see a direct link between "if generate_mail returned an error"
   and "if ... an empty input".  What if generate_mail started its output
   but then failed halfway?  We have some output so the send_mail won't be
   fed empty, but $? would be not zero, so the patch is testing a
   different condition from what the log message claims to be checking.

People who do use this script and people who have worked on it may have
other more useful comments.

Thanks.

```
