Subject: Re: [PATCH] t3706: Add test for wildcard vs literal pathspec

From: Junio C Hamano <[email-masked]>
Date: Mon, 14 Apr 2025 14:42:20 -0700

K Jayatheerth <[email-masked]> writes:

> When 'git add <pattern>' is run, and a file exists whose literal
> name matches <pattern> (e.g., a file named "f*" when running
> 'git add f*'), Git should treat the pattern as a wildcard
> matching multiple files, rather than just adding the literal file.

This pretends to be a separate and independent patch, but it in
reality depends on the other change to dir.c, doesn't it?

Either make them into two-patch series, or better yet make them into
a single patch, perhaps?

> Add a test case that:
>  1. Creates files 'f*', 'f**', and 'foo'.
>  2. Verifies that 'git add "f\*\*"'
>     correctly adds only the literal file 'f**'.
>  3. Verifies that 'git add 'f*'' (quoted to prevent shell expansion)
>     correctly adds 'f*', 'f**', and 'foo' by treating 'f*' as a
>     wildcard.

That is something easily can be read from the script itself.  Don't
repeat what the code does, UNLESS the explanation helps readers to
understand what the code does NOT tell them (like, "why does the
code does what it does?").

> Covering these the test adds 5 cases.
>
> Signed-off-by: K Jayatheerth <[email-masked]>
> ---
>  t/meson.build                   |  1 +
>  t/t3706-add-wildcard-literal.sh | 44 +++++++++++++++++++++++++++++++++
>  2 files changed, 45 insertions(+)
>  create mode 100755 t/t3706-add-wildcard-literal.sh

I am not sure if this should be done as a new test script and covers
only "git add".  Is "git add" the only code paths that triggers
do_match_pathspec()?  If there are many, it may make sense to have a
new script (like this patch does) and cover all of these commands
that are corrected by the previous patch.  If not, and if it truly
affects only "git add", then it would make more sense to add to an
existing test script that covers "git add".

> +test_expect_success 'setup: create files and initial commit' '
> +    mkdir testdir &&
> +    >testdir/f\* &&
> +    >testdir/f\*\* &&
> +    >testdir/foo &&
> +    git add testdir &&
> +    git commit -m "Initial setup with literal wildcard files"
> +'
> +
> +test_expect_success 'clean slate before testing wildcard behavior' '
> +    git rm -rf testdir &&
> +    git commit -m "Clean state"
> +'

What's the point of the above two test pieces?  The initial commit
is not used in any way after it gets created, the wildcard pathspec,
which is the topic of this test, is not involved in its creation in
any way, and everything gets removed before the real test after this
step starts.  Not complaining, but showing puzzlement, as I truly do
not see the point of these two steps.

> +test_expect_success 'recreate files to test add behavior' '
> +    mkdir testdir &&
> +    >testdir/f\* &&
> +    >testdir/f\*\* &&
> +    >testdir/foo
> +'
> +
> +test_expect_success 'quoted literal: git add "f\\*\\*" adds only f**' '
> +    git reset &&

What is the point of this "reset"?  Since the last commit, the tree
of HEAD matches the index, and nobody touched the index after that.

> +    git add "testdir/f\\*\\*" &&

OK.  We are giving the pathspec with asterisks quoted in it, to make
sure the asterisks are not interpreted as glob.  So there is only
one path that gets added in the end, and that is what is tested.

That's a good test (except that I do not understand what the "reset"
at the beginning is trying to do).

> +    git ls-files >actual &&
> +    echo "testdir/f**" >expected &&
> +    test_cmp expected actual
> +'
> +
> +test_expect_success 'wildcard: git add f* adds f*, f** and foo' '
> +    git reset &&
> +    git add '\''testdir/f*'\'' &&

Why not just

	git add "testdir/f*" &&

to match the previous one?

> +    git ls-files | sort >actual &&

"git" command on the left hand side of a pipe means even if it
fails, its exit status is hidden.  Also, the order ls-files emits
paths is well defined (sorted in the byte-value order regardless of
locale), so piping its output through sort loses information
unnecessarily (you wouldn't notice if ls-files started emitting its
paths in an incorrect order).

Since you use these three pathnames in this order quote often in
this script, why not do this once upfront:

	cat >list-of-files <<-\EOF &&
	testdir/f*
	testdir/f**
        testdir/foo
	EOF

and then use the list of files in later steps like so:

	while read path
	do
		>"$path"
	done <list-of-files &&

	git ls-files >actual &&
	test_cmp list-of-files actual

> +    printf "%s\n" "testdir/f*" "testdir/f**" "testdir/foo" | sort >expected &&
> +    test_cmp expected actual
> +'
> +
> +test_done
> \ No newline at end of file

Please make sure your files are properly formatted.  Perhaps we
should add whitespace error class 'incomplete-lines' that catches
this, and enable it globally, or something?