grep: fix worktree case in submodules
[gitweb.git] / t / t7814-grep-recurse-submodules.sh
index 7184113b9b2b381a6e7e0def8a8c1ba06771f8a6..48f2b13e7d7905b092ed852c38a2534d3ee31bf2 100755 (executable)
@@ -380,4 +380,37 @@ test_expect_success 'grep --recurse-submodules should pass the pattern type alon
        fi
 '
 
+test_expect_success 'grep --recurse-submodules with submodules without .gitmodules in the working tree' '
+       test_when_finished "git -C submodule checkout .gitmodules" &&
+       rm submodule/.gitmodules &&
+       git grep --recurse-submodules -e "(.|.)[\d]" >actual &&
+       cat >expect <<-\EOF &&
+       a:(1|2)d(3|4)
+       submodule/a:(1|2)d(3|4)
+       submodule/sub/a:(1|2)d(3|4)
+       EOF
+       test_cmp expect actual
+'
+
+reset_and_clean () {
+       git reset --hard &&
+       git clean -fd &&
+       git submodule foreach --recursive 'git reset --hard' &&
+       git submodule foreach --recursive 'git clean -fd'
+}
+
+test_expect_success 'grep --recurse-submodules without --cached considers worktree modifications' '
+       reset_and_clean &&
+       echo "A modified line in submodule" >>submodule/a &&
+       echo "submodule/a:A modified line in submodule" >expect &&
+       git grep --recurse-submodules "A modified line in submodule" >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'grep --recurse-submodules with --cached ignores worktree modifications' '
+       reset_and_clean &&
+       echo "A modified line in submodule" >>submodule/a &&
+       test_must_fail git grep --recurse-submodules --cached "A modified line in submodule" >actual 2>&1 &&
+       test_must_be_empty actual
+'
 test_done