Subject: Re: [PATCH v2 04/12] fetch tests: double quote a variable for interpolation

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Tue, 23 Jan 2018 00:04:28 +0100

On Mon, Jan 22 2018, Junio C. Hamano jotted:

> Ævar Arnfjörð Bjarmason  <[email-masked]> writes:
>
>> If the $cmdline variable contains multiple arguments they won't be
>> interpolated correctly since the body of the test is single quoted. I
>> don't know what part of test-lib.sh is expanding variables within
>> single-quoted strings,...
>
>     dothis='echo whatever $IFS separated strings'
>     test_expect_success label '
>             $dothis
>     '
>
> works because test_expect_success ends up beint a glorified 'eval'
> and it sees the value of $dothis.
>
>> but interpolating this inline is the desired
>> behavior here.
>
> I am not sure what you meant by this, though.

Looking into this again:

    myvar="a b 'c' \"d\" \"\\\"e\\\"\""
    test_expect_success 'blah' '
    	/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- $myvar &&
    	/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- '"$myvar"' &&
    	echo $myvar &&
    	false
    '

Produces:

    Initialized empty Git repository in /home/avar/g/git/t/trash directory.t5510-fetch/.git/
    expecting success:
    	/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- $myvar &&
    	/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- a b 'c' "d" "\"e\"" &&
    	echo $myvar &&
    	false

    + /usr/bin/perl -MData::Dumper -wE say Dumper \@ARGV -- a b 'c' "d" "\"e\""
    $VAR1 = [
              'a',
              'b',
              '\'c\'',
              '"d"',
              '"\\"e\\""'
            ];

    + /usr/bin/perl -MData::Dumper -wE say Dumper \@ARGV -- a b c d "e"
    $VAR1 = [
              'a',
              'b',
              'c',
              'd',
              '"e"'
            ];

    + echo a b 'c' "d" "\"e\""
    a b 'c' "d" "\"e\""
    + false
    error: last command exited with $?=1
    not ok 1 - blah
    #
    #		/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- $myvar &&
    #		/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- a b 'c' "d" "\"e\"" &&
    #		echo $myvar &&
    #		false
    #

I.e. the desired effect is to get variables like the refspec like/this
not 'like/this'. I could also just apply this on top, which gives the
same end result, but now I wonder if starting some args with
e.g. unescaped + and with : and * in the string is portable:

    diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
    index 48f49b613a..2d311059e9 100755
    --- a/t/t5510-fetch.sh
    +++ b/t/t5510-fetch.sh
    @@ -587 +587 @@ test_configured_prune () {
    -			git fetch '"$cmdline"' &&
    +			git fetch $cmdline &&
    @@ -621 +621 @@ test_configured_prune unset unset unset unset kept   pruned \
    -	"--prune origin 'refs/tags/*:refs/tags/*'"
    +	"--prune origin refs/tags/*:refs/tags/*"
    @@ -623 +623 @@ test_configured_prune unset unset unset unset pruned pruned \
    -	"--prune origin 'refs/tags/*:refs/tags/*' '+refs/heads/*:refs/remotes/origin/*'"
    +	"--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
    @@ -641 +641 @@ test_configured_prune false false unset unset kept   pruned \
    -	"--prune origin 'refs/tags/*:refs/tags/*'"
    +	"--prune origin refs/tags/*:refs/tags/*"
    @@ -643 +643 @@ test_configured_prune false false unset unset pruned pruned \
    -	"--prune origin 'refs/tags/*:refs/tags/*' '+refs/heads/*:refs/remotes/origin/*'"
    +	"--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
    @@ -661 +661 @@ test_configured_prune true  true  unset unset kept   pruned \
    -	"--prune origin 'refs/tags/*:refs/tags/*'"
    +	"--prune origin refs/tags/*:refs/tags/*"
    @@ -663 +663 @@ test_configured_prune true  true  unset unset pruned pruned \
    -	"--prune origin 'refs/tags/*:refs/tags/*' '+refs/heads/*:refs/remotes/origin/*'"
    +	"--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
    @@ -684 +684 @@ test_configured_prune true  false true  false kept   kept   ""
    -# When --prune-tags is supplied it's ignored if an explict refspec is
    +# When --prune-tags is supplied its ignored if an explict refspec is
    @@ -687 +687 @@ test_configured_prune unset unset unset unset pruned kept \
    -	"--prune --prune-tags origin '+refs/heads/*:refs/remotes/origin/*'"
    +	"--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
    @@ -689 +689 @@ test_configured_prune unset unset true  unset pruned kept \
    -	"--prune --prune-tags origin '+refs/heads/*:refs/remotes/origin/*'"
    +	"--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
    @@ -691 +691 @@ test_configured_prune unset unset unset true pruned  kept \
    -	"--prune --prune-tags origin '+refs/heads/*:refs/remotes/origin/*'"
    +	"--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"

>> -			git fetch $cmdline &&
>> +			git fetch '"$cmdline"' &&
>
> Would this work with cmdline that needs to be quoted for the
> resulting shell script to be syntactically correct (e.g. cmdline
> with a single dq in it)?  By stepping out of sq pair, you are
> allowing/asking the shell that forms test_expect_success command
> line arguments to interpolate cmdline, instead of asking the shell
> that evals test_expect_success with its command line argument
> strings.
>
> In other words, I suspect that the caller of test_configured_prune
> now must sq_quote the cmdline arguments it passes to this helper,
> i.e.
>
> 	cmdline="$(git rev-parse --sq-quote arg1 'arg"2' arg3)"
> 	test_configured_prune ... "$cmdline" ...
>
> for this patch to be correct.

Sorry at this point I'm confused about this whole thing. This doesn't
work and overquotes:

    @@ -559,2 +569,3 @@ test_configured_prune () {

    +	cmdline_q="$(git rev-parse --sq-quote $cmdline)"
     	test_expect_success "prune fetch.prune=$1 remote.origin.prune=$2 fetch.pruneTags=$3 remote.origin.pruneTags=$4${7:+ $7}; branch:$5 tag:$6" '
    @@ -586,3 +597,4 @@ test_configured_prune () {

    -			git fetch '"$cmdline"' &&
    +			/usr/bin/perl -MData::Dumper -wE say\ Dumper\ \\@ARGV -- '"$cmdline_q"' &&
    +			git fetch '"$cmdline_q"' &&
     			case "$expected_branch" in

So does just using $cmdline_q without making the calling shell
interpolate it. Is there an idiom I should be following that I'm missing
here?