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

From: K Jayatheerth <[email-masked]>
Date: Sat, 12 Apr 2025 23:10:51 +0530

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.

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.

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

diff --git a/t/meson.build b/t/meson.build
index 8b3aed14ea..e9cd9da8a2 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -417,6 +417,7 @@ integration_tests = [
   't3703-add-magic-pathspec.sh',
   't3704-add-pathspec-file.sh',
   't3705-add-sparse-checkout.sh',
+  't3706-add-wildcard-literal.sh',
   't3800-mktag.sh',
   't3900-i18n-commit.sh',
   't3901-i18n-patch.sh',
diff --git a/t/t3706-add-wildcard-literal.sh b/t/t3706-add-wildcard-literal.sh
new file mode 100755
index 0000000000..0fc27b28ac
--- /dev/null
+++ b/t/t3706-add-wildcard-literal.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+test_description='git add: wildcard must not be shadowed by literal filename'
+
+. ./test-lib.sh
+
+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"
+'
+
+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 &&
+    git add "testdir/f\\*\\*" &&
+    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*'\'' &&
+    git ls-files | sort >actual &&
+    printf "%s\n" "testdir/f*" "testdir/f**" "testdir/foo" | sort >expected &&
+    test_cmp expected actual
+'
+
+test_done
\ No newline at end of file
-- 
2.49.0