Subject: Re: [PATCH v3] git-send-email: add option to specify sendmail command

From: Junio C Hamano <[email-masked]>
Date: Fri, 14 May 2021 13:25:09 +0900

Gregory Anders <[email-masked]> writes:

> Use a block scoped variable to print the sendmail invocation at the end 
> of the 'send_message' subroutine. Assigning directly to $sendmail_cmd 
> (as in the v2 patch) causes some bizarre problems; namely, it seems to 
> affect the value of $sendmail_cmd that is read at earlier points in the 
> same subroutine, which causes test invocations of the form
>
>     git send-email --smtp-server="$(pwd)/fake.sendmail"
>
> to fail. The value passed to --smtp-server was assigned to $sendmail_cmd 
> at the end of the 'send_message' subprocedure, but somehow this caused 
> the 'if (defined $sendmail_cmd)' condition earlier in the subproc to 
> evaluate to true.

Are you talking about the use of $sm that is local to the debug
output?  I think leaving $sendmail_cmd intact by using a separate
variable is the right choice.  Isn't the problem you observed a
consequence of send_message() getting called once for each message,
so assigning to $sendmail_cmd in the function for the first
invocation of the function would change its value for the second
invocation?

Also, if we have been using

	--smtp-server=$(pwd)/fake.sendmail

we cannot expect to use the same value like this:

	--sendmail-cmd=$(pwd)/fake.sendmail

because we deliberately add a space in the $(pwd) by choosing the
name of the test directory to be "trash directory.something".  We'd
need to do something like

	--sendmail-cmd='$(pwd)/fake.sendmail'

so that the shell sees '$(pwd)/fake.sendmail' literally and runs pwd
to find out what the path to the program is, I would think.

> diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
> index 65b3035371..583fbba410 100755
> --- a/t/t9001-send-email.sh
> +++ b/t/t9001-send-email.sh
> @@ -2148,6 +2148,37 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
>  	test_cmp expected-list actual-list
>  '
>  
> +test_expect_success $PREREQ 'test using command name with --sendmail-cmd' '
> +	clean_fake_sendmail &&
> +	PATH="$(pwd):$PATH" \
> +	git send-email \
> +		--from="Example <[email-masked]>" \
> +		--to=[email-masked] \
> +		--sendmail-cmd="fake.sendmail" \
> +		HEAD^ &&
> +	test_path_is_file commandline1
> +'

Nice demonstration of the "we no longer need an absolute pathname"
feature.

> +test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' '
> +	clean_fake_sendmail &&
> +	git send-email \
> +		--from="Example <[email-masked]>" \
> +		--to=[email-masked] \
> +		--sendmail-cmd="\"$(pwd)/fake.sendmail\" -f [email-masked]" \
> +		HEAD^ &&
> +	test_path_is_file commandline1
> +'

Hmph, if $(pwd) has a double quote character in it, this may not
work as expected, as the shell that is expanding the command line
arguments for "git send-email" would see $(pwd), expand it and our
program will see

    "/path/with/d"quote/git/t/trash directory.9001/fake.sendmail" -f [email-masked]

as the value of --sendmail-cmd, which would not interpolate well,
no?

We want the shell that eats the command line of 'git send-email' to see

	--sendmail-cmd='$(pwd)/fake.sendmail'\" -f [email-masked]"

and because this is inside a sq pair, it would become

	--sendmail-cmd='\''$(pwd)/fake.sendmail'\''\" -f [email-masked]"

after we replace each sq with '\'', or something like that, perhaps?

> +test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' '
> +	clean_fake_sendmail &&
> +	git send-email \
> +		--from="Example <[email-masked]>" \
> +		--to=[email-masked] \
> +		--sendmail-cmd="f() { \"$(pwd)/fake.sendmail\" \"\$@\"; };f" \
> +		HEAD^ &&
> +	test_path_is_file commandline1
> +'

Nice demonstration of how a bit of scripting can be used.

>  test_expect_success $PREREQ 'invoke hook' '
>  	mkdir -p .git/hooks &&

Thanks.